Last active
February 25, 2016 14:58
-
-
Save CTimmerman/50d184a38da6c2884a17 to your computer and use it in GitHub Desktop.
Loop large arrays in JS without hogging the CPU
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
# synchronous | |
a = 'abc'; for i in [0...a.length] then console.log i, a[i] | |
# asynchronous | |
a = 'abc'; i = 0; x = setInterval -> | |
if i < a.length then console.log i, a[i]; ++i else clearInterval x | |
, 1000 |
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
// synchronous | |
var a = 'abc'; for(var i = 0; i < a.length; ++i) console.log(i, a[i]) | |
// asynchronous | |
var a = 'abc'; var i = 0; var x = setInterval(function(){if(i < a.length){console.log(i, a[i]); ++i}else{clearInterval(x)}}, 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment