Last active
December 30, 2015 09:39
-
-
Save noonat/7811000 to your computer and use it in GitHub Desktop.
This file contains 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
// really slow: ~50s | |
try { | |
return ret.concat.apply([], ret); | |
} catch (e) { | |
if (e instanceof RangeError && ret.reduce) { | |
return ret.reduce(function(a, b) { | |
return a.concat(b); | |
}, []) | |
} else { | |
throw e; | |
} | |
} |
This file contains 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
// also slow: ~45s | |
try { | |
return ret.concat.apply([], ret); | |
} catch (e) { | |
if (e instanceof RangeError) { | |
var flat = []; | |
for (var i = 0, il = ret.length; i < il; ++i) { | |
flat = flat.concat(ret[i]); | |
} | |
return flat; | |
} else { | |
throw e; | |
} | |
} |
This file contains 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
// much better! ~2ms w/ native isArray, ~15ms w/out | |
try { | |
return ret.concat.apply([], ret); | |
} catch (e) { | |
if (e instanceof RangeError) { | |
var flat = [], isArray = Array.isArray || function(obj) { | |
return toString.call(obj) == '[object Array]'; | |
}; | |
for (var i = 0, il = ret.length; i < il; ++i) { | |
var item = ret[i]; | |
if (isArray(item)) { | |
flat = flat.concat(item); | |
} else { | |
flat.push(item); | |
} | |
} | |
return flat; | |
} else { | |
throw e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment