My each function

JavaScript performance comparison

Test case created

Preparation code

<script type='text/javascript'>
(function( window ) {
"use strict";

var jsBorer = function(selector) {
  if(!selector) return false;
  if(this instanceof jsBorer) {
    this.target = (typeof selector==='string')?document.getElementById(selector):selector;
    return this;
  } else
    return (typeof selector==='string')?document.getElementById(selector):selector;
}
       
jsBorer.tag= function( par ) {
  if (!par) return "";
  if (typeof par === 'string') {
    var a = par;
    par = {};
    par.tag = a
  };
  if (typeof par !== 'object' || !par.tag) return "";
  var r = []; r.push("<"+par.tag)
  for (var key in par ) if(key!=="tag"&&key!=="html"&&key!=="endtag"&&key!=="par"&&key!=="par2"&&key!=="val"&&key!=="sel"&&key!=="idxval"&&typeof key !== 'object') r.push( " " + key + "=\"" + par[key] + "\"" );
  r.push( ((par.par)?par.par:"")+((par.par2)?par.par2:"")+">" );
  if(par.html) r.push(par.html);
  if(par.endtag) r.push("</"+par.tag+">");
  return r.join("");
}

//idxval=boolean (value as index)
jsBorer.tagselect= function( par ) {
  if (!par || typeof par !== 'object' || !par.val) return "";
  var r = [];par['tag']='select';r.push( $$.tag(par));
  if(par.val instanceof Array){
        var a=par.val,k=0;
        par.val={};
        a.map(function(item){par.val[(($$.istrue(par.idxval))?k:item)]=item;k++})
  }
  for (var key in par.val ) {
        r.push( $$.tag({
       tag:'option'
      ,value: key
      ,par:(par.sel&&''+par.sel===key)?' selected=\"true\"':''
      ,html: par.val[key]
      ,endtag: true
    }) );
  }
  r.push('</select>');
  return r.join("");
}

//undftrue=boolean (undefined = true)
jsBorer.istrue= function(v,undftrue){if(undftrue)return((typeof v==='undefined'||v)?true:false);else return((typeof v!=='undefined'&&v)?true:false)}
jsBorer.isdate= function(v){return(v&&typeof v.getMonth==='function')}

//count oblect elements
jsBorer.objlength=function(a){var c=0;for(var i in a){if(a.hasOwnProperty(i))c++;}return c}

jsBorer.keyw=function(a){a.keyw=function(e){return(window.event)?event.keyCode:e.which}}

jsBorer.isNumeric=function(i){return (i-0)==i&&i.length>0}

jsBorer.each=function(a,f){var l=a.length,i=0;for(;i!==l;i++)f(i,a[i]);}

window.jsBorer = window.$$ = jsBorer;
})(window);


function each(a,f){var l=a.length,i=0;for(;i!==l;i++)f(i,a[i]);}


var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
var res = 0;


</script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jQuery
$.each(myArray, function(i,v){
  res += v;  
})
 
pending…
My
$$.each(myArray, function(i,v){
  res += v;  
})
 
pending…
Alone fn
each(myArray, function(i,v){
  res += v;  
})
 
pending…

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

Compare results of other browsers

0 comments

Add a comment