Recursion vs For Loop vs While Loop (v9)

Revision 9 of this benchmark created on


Description

Fastest method to function over array

Preparation HTML

<script>
  var nums = [];
for (var i = 0; i <= 1000; i++) {
    nums.push(~~(Math.random()*10000));
}
function thefunc(item, array, varsObj){
    varsObj.total += item;
}
var vars = {total:0}
function evalmethodset(maxsize) {
    var ti = maxsize;
    var x = {
        thestart: function(thearr, func, vars){
            var total = 0;
            var i = -1;
        },
        theprocess: function(thearr, total, i, func, vars){
            if(thearr[i++]){
                func(thearr[i], thearr, vars);
            }else{
                return vars;
            }
        },
        thefinish: function(vars){
            return vars;
        }
    };
    function newFilledArray(length, val) {
        for (var array = [],i = 0; i < length; i++) {
            array[i] = val;
        }
        return array;
    }
    var thereg = /(function\s?)([^\.])([\w|,|\s|-|_|\$]*)(.+?\{)([^\.][\s|\S]*(?=\}))/;
    var thestring = '';
    var theprocessstart = x.thestart.toString().match(thereg);
    var theprocesscode = x.theprocess.toString().match(thereg);
    var theprocessfinish = x.thefinish.toString().match(thereg);
    thestring += theprocessstart[5];
    var i = 0;
    for(i;i<=ti;i++){
        thestring += theprocesscode[5];

    }
    i = 0;
    thestring += theprocessfinish[5];
    var thefunc;
    eval('thefunc = function('+theprocessstart[3]+'){'+thestring+'}');
    return thefunc;
}
evalmethod = evalmethodset(1000);

  function sum_recurse_set(arr, func, vars) {
    var ti = arr.length - 1, i = 0, total = 0, thearr = arr, rtn;
    function go(total, thearr, i,go, func, vars){
        func(thearr[i], thearr, vars);
        if(i !== ti){
            i++;
             return go(total, thearr, i,go);
        }else{
            return vars;
        }
    }
   function foo(thearr, func, vars){
      return go(total, thearr, i,go, func, vars);
   }
   return foo;
}
  sum_recurse = sum_recurse_set(nums);

  function sum_while(arr, func, vars) {
   var total = 0,
       i = 0,
       len = arr.length;
  
   while (i < len) {
    func(arr[i], arr, vars)
   }
  
   return vars;
  }
  
  function sum_for(arr, func, vars) {
   var total = 0,
       len = arr.length;
  
   for (var i = 0; i < len; i++) {
    func(arr[i], arr, vars);
   }
  
   return vars;
  }



function experiment(arr, func, vars){
  var x = new Array(arr.length), i = -1, sum = 0;
  while (x = arr.pop()) {
     func(arr[i++], arr, vars);
  }
  return vars;
}
var cachedArr = new Array(nums.length);
function cachedLengthArray(arr, lenArr, func, vars){
  var x = lenArr.slice(0), i = -1, sum = 0;
  while (x = arr.pop()) {
     func(arr[i++], arr, vars);
  }
  return vars;
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
While Loop
sum_while(nums, thefunc, vars);
ready
For Loop
sum_for(nums, thefunc, vars);
ready
eval ninja method
evalmethod(nums, thefunc, vars);
ready
pop method destructive - removed useless
 
ready
pop method non destructive no use for func pass removed
 
ready
Pop method expected behaviour
experiment(nums, thefunc, vars);
ready
Pop method slice cached
cachedLengthArray(nums, cachedArr, thefunc, vars);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.