Skip to content

Instantly share code, notes, and snippets.

@CTimmerman
Last active February 25, 2016 14:58
Show Gist options
  • Save CTimmerman/50d184a38da6c2884a17 to your computer and use it in GitHub Desktop.
Save CTimmerman/50d184a38da6c2884a17 to your computer and use it in GitHub Desktop.
Loop large arrays in JS without hogging the CPU
# 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
// 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