Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upProposal: use Array.isArray instead of instanceof Array to handle crazy JS better #487
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
What are the differences in performance of these two approaches? |
evancz
changed the title from
Json decode failing for arrays in NWJS project
to
Proposal: use Array.isArray instead of instanceof Array to handle crazy JS better
Jun 25, 2016
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
lukewestby
Aug 11, 2016
Member
http://jsbin.com/muqaciyozi/1/edit?js,console
I did some simple benchmarking with benchmark.js in various browsers. Here toString refers to the technique Object.prototype.toString.call(obj) === '[object Array]'. This is included with the existing instanceof and the proposed Array.isArray. The tradeoffs seem to be: instanceof is the fastest but does not behave across execution contexts. Array.isArray is consistent but slower than instanceof and also not supported prior to IE9. Using Object.prototype.toString is consistent across contexts, broadly supported in browsers, but is notably slower than the other two options.
If switching to Array.isArray is desirable then it is possible to polyfill it with the toString method for IE8 and below.
Mean execution times according to benchmark.js:
[] instanceof Array |
{} instanceof Array |
Array.isArray([]) |
Array.isArray({}) |
toString([]) === '[object Array]' |
toString({}) === '[object Array]' |
|
|---|---|---|---|---|---|---|
| Chrome 52 | 1.7876434065161444e-8 | 2.330498325001016e-8 | 2.436932813655087e-8 | 3.003167409610475e-8 | 6.593245721914357e-7 | 4.105239635961423e-7 |
| Safari 9.1 | 4.4020788002132974e-8 | 3.5085045152805906e-8 | 5.478782940038529e-8 | 4.453426066743071e-8 | 8.996925572126374e-8 | 6.33213332061942e-8 |
| Firefox 44 | 7.198862145315244e-10 | 7.910334331974017e-10 | 7.209425173911308e-10 | 8.002760240146521e-10 | 3.063021952664718e-8 | 2.2586274411698193e-8 |
| IE 11 | 1.64349e-7 | 6.09866e-8 | 2.00234e-7 | 9.76242e-8 | 3.03752e-7 | 1.28477e-7 |
|
http://jsbin.com/muqaciyozi/1/edit?js,console I did some simple benchmarking with benchmark.js in various browsers. Here If switching to Mean execution times according to benchmark.js:
|
AZon8 commentedJan 18, 2016
I had some problems with decoding json in a situation where i was passing in node.js Arrays.
The problem originated because of this :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof
The solution for me was to replace all the Native/Json.js
value instanceof ArraywithArray.isArray(value)