Nodejs Array ForEach forEach1( callback )

Here you can find the source of forEach1( callback )

Method Source Code

Array.prototype.forEach1 = function ( callback ) {
        for (var j = 0, k ; k=this[j++]; ) {  callback(k,j,this) ; }
}
Array.prototype.forEach2 = function ( callback ) {
        for (var j = 0 ; j < this.length; j++ ) {  callback(this[j],j,this) ; }
}
Array.prototype.forEach3 = function ( callback ) {
        var j = 0 ; while( this[j++] ) {  callback(this[j],j,this) ; }
}
var a = [1,2,3,4,5,6,7,8,9,0] ;
function unit ( method ) 
{
   var s = [] ;/*from  w  w  w .  ja  v a  2  s  .c  o m*/
    a[method]( function (v,i,a) { s.push(i+":"+v); } )
  return s ;
}

function loop ( size, method ) {
var t1 = new Date() ;
 while ( size--) { unit(method); }
return (new Date()) - t1 ;
}

loopall = function ( ) {
 var s = [] ;
 var j = 0, method ; while ( method = arguments[j++] ) {
  s.push( method + ": " +  loop( 1e4 , method )  + " ms " ) ;
 }
   return s ;
} ( "forEach1", "forEach2", "forEach3" ) .join("\n") ;

Related

  1. forEach(func)
    Array.prototype.forEach = function (func) {
        var length = this.length;
        for (var i = 0; i < length; i++) {
            func(this[i]);
    
  2. forEach(func, thisObj)
    Array.prototype.forEach = Array.prototype.forEach || (Array.prototype.forEach = function(func, thisObj) {
      for (var i=0, l=this.length; i<l; ++i) {
        func.call(thisObj, this[i], i, this);
      };
    });
    
  3. forEach(iterator)
    Array.prototype.forEach = function(iterator) {
      for (var i = 0, len = this.length; i < len; i++) {
        iterator(this[i], i);
    };
    
  4. forEach(predicate)
    Array.prototype.forEach = function(predicate)
      for (var i = 0; i < this.length; i++)
        predicate(this[i]);
    Array.prototype.contains = function(o)
      return this.indexOf(o) >= 0;
    
  5. forEach.call(items, (item)>
    items = document.querySelectorAll('x-markdown')
    Array.prototype.forEach.call(items, (item)=>{
      html = marked(item.textContent)
      item.innerHTML = html
    })
    
  6. forEach2(a)
    Array.prototype.forEach2 = function (a) {
      for (var b = 0; b < this.length; ++b) a(this[b], b, this)
    };
    Array.prototype.map2 = function (a) {
      var l = this.length;
      var array = new Array(l),
        i = 0;
      for (; i < l; i++) {
        array[i] = a(this[i], i)
    ...
    
  7. forEachCallback(callback, finishCallback)
    'use strict';
    Array.prototype.forEachCallback = function(callback, finishCallback) {
      var current = 0;
      var self = this;
      var next = function() {
        if (!self) {
          console.log("Something went wrong...");
          throw('No self!');
          return;
    ...
    
  8. forEachSync(callback,compliteCallBack)
    var async = require('async');
    var worker = function(array,callback,compliteCallBack){
      var index = 0;
      var q = async.queue(function(item,cb){
        var cback = function(){
          index = index+1;
          cb();
        };
        callback(item,index,cback)
    ...
    
  9. forEachSync(callback,compliteCallBack)
    var async = require('async');
    var worker = function(array,callback,compliteCallBack){
      var index = 0;
      var q = async.queue(function(item,cb){
        var cback = function(){
          index = index+1;
          cb(); 
        };
        process.nextTick(function(){
    ...