jQuery's isArray vs instanceof Array (v2)

Revision 2 of this benchmark created by Jens Roland on


Description

Is jQuery.isArray comparably fast to obj instanceof Array?

EDIT: added a couple of methods I picked up a while back which are more robust than instanceof in special cases.

The problem is, you cannot reliably test for arrays using instanceof. The following check returns false: ((new Array) instanceof Array)

While the following returns true: ((new Array) instanceof Object)

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  function jq_array() {
   return jQuery.isArray([]);
  }
  
  function instof() {
   return ([] instanceof Array);
  }
  
  function constr() {
   return ([].constructor == Array);
  }
  
  function tostringcall() {
   Object.prototype.toString.call([]) === '[object Array]';
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery
jq_array();
ready
instanceof
instof();
ready
constructor
constr();
ready
toString.call
tostringcall();
ready

Revisions

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