Last active
January 1, 2016 04:39
-
-
Save ysmood/8093548 to your computer and use it in GitHub Desktop.
break_deep_loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun_closure = function(n) { | |
| for (i = 0; i < n; i++) | |
| (function(){ | |
| for (j = 0; j < n; j++) | |
| for (k = 0; k < n; k++) { | |
| if (j * k > n) return; | |
| j + k; | |
| } | |
| })() | |
| } | |
| fun_flag = function(n) { | |
| is_break = false | |
| for (i = 0; i < n; i++) | |
| for (j = 0; j < n; j++) | |
| if (is_break) break; | |
| for (k = 0; k < n; k++) { | |
| if (j * k > n) { | |
| is_break = true; | |
| break; | |
| } | |
| j + k; | |
| } | |
| } | |
| fun_label = function(n) { | |
| for (i = 0; i < n; i++) | |
| jump_out: | |
| for (j = 0; j < n; j++) | |
| for (k = 0; k < n; k++) { | |
| if (j * k > n) break jump_out; | |
| j + k; | |
| } | |
| } | |
| test = function (fun, title) { | |
| time_stamp = Date.now(); | |
| fun(5000); | |
| console.log('>> ' + title + ': ' + (Date.now() - time_stamp) + 'ms'); | |
| } | |
| test(fun_closure, 'closure'); | |
| test(fun_flag, 'flag'); | |
| test(fun_label, 'label'); |
HerringtonDarkholme
commented
Dec 23, 2013
Author
Closure's performance is not what I want.
The test result is:
>> closure: 3800ms
>> flag: 254ms
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment