JavaScript, Node.js: is Array.forEach asynchronous? -


i have question regarding native array.foreach implementation of javascript: behave asynchronously? example, if call:

[many many elements].foreach(function () {lots of work do}) 

will non-blocking?

no, blocking. have @ specification of algorithm.

however maybe easier understand implementation given on mdn:

if (!array.prototype.foreach) {   array.prototype.foreach = function(fun /*, thisp */)   {     "use strict";      if (this === void 0 || === null)       throw new typeerror();      var t = object(this);     var len = t.length >>> 0;     if (typeof fun !== "function")       throw new typeerror();      var thisp = arguments[1];     (var = 0; < len; i++)     {       if (i in t)         fun.call(thisp, t[i], i, t);     }   }; } 

if have execute lot of code each element, should consider use different approach:

function processarray(items, process) {     var todo = items.concat();      settimeout(function() {         process(todo.shift());         if(todo.length > 0) {             settimeout(arguments.callee, 25);         }     }, 25); } 

and call with:

processarray([many many elements], function () {lots of work do}); 

this non-blocking then. example taken high performance javascript.

another option might web workers.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -