Check isArray

JavaScript performance comparison

Revision 3 of this test case created by Mario DE WEERD

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = ["tada", "pula"];
  var str = "ping";
  window.penalty=function()
   {
    var millis=10;
    var date = new Date();
    var curDate = null;
    do { curDate = new Date(); }
    while(curDate-date < millis);
  }
  /** The following adds methods/variables to the object types.
  This method does not work for 'undefined' values though */
  Object.prototype.isArray=function(){return false;}
  Array.prototype.isArray=function(){return true;}
  Object.prototype._isArray=false;
  Array.prototype._isArray=true;

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
4. isArray()
if(!Array.isArray(arr)||Array.isArray(str))penalty();
pending…
3. typeof and instance
if(!(typeof arr === "array" || arr instanceof Array)
   ||(typeof str === "array" || str instanceof Array))penalty();
pending…
5. toString()
if(!(Object.prototype.toString.call(arr) === '[object Array]')
||(Object.prototype.toString.call(str) === '[object Array]')
)penalty();
pending…
Z. typeof
if(!(typeof arr === "array")
||(typeof str === "array"))penalty();
pending…
2. instanceof
if(!(arr instanceof Array)
||(str instanceof Array)
)penalty();
pending…
6. Func isArray
/** This would not work on undefined, but does for defined variables. */
if(!arr.isArray()
||str.isArray()
)penalty();
pending…
1. var _isArray
/** This would not work on undefined though, but does for defined variables. */
if(!arr._isArray
||str._isArray
)penalty();
pending…
7. map
if(!(arr.map==[].map)
||(str.map==[].map)
)penalty();
pending…

Compare results of other browsers

Revisions

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

0 Comments