Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
comparing arrays with lodash and not
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser:
Chrome 121
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
lodash isEqual
419972.1 Ops/sec
arraysEqual
1915.1 Ops/sec
JSON.stringify (WONT WORK WITH OBJECTS)
1928.0 Ops/sec
Option 2
2758.3 Ops/sec
option 3
23672.5 Ops/sec
Option 4
1869.7 Ops/sec
HTML Preparation code:
<script src="https://lodash.com/vendor/cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
Script Preparation code:
var a=[]; var b=[]; for (var i = 0; i < 1024; i++) { a[i] = i; b[i] = i; } // ############################################### // # https://stackoverflow.com/a/16436975/2601293 // ############################################### function arraysEqual(a, b) { if (a === b) return true; if (a == null || b == null) return false; if (a.length != b.length) return false; // If you don't care about the order of the elements inside // the array, you should sort both arrays here. // Please note that calling sort on an array will modify that array. // you might want to clone your array first. for (var i = 0; i < a.length; ++i) { if (a[i] !== b[i]) return false; } return true; } // ############################################### // # https://stackoverflow.com/a/10316616/2601293 (Option 1) // ############################################### function arraysEqual(a1,a2) { /* WARNING: arrays must not contain {objects} or behavior may be undefined */ return JSON.stringify(a1)==JSON.stringify(a2); } // ######################################################### // # https://stackoverflow.com/a/10316616/2601293 (Option 2) // ######################################################### // generally useful functions function type(x) { // does not work in general, but works on JSONable objects we care about... modify as you see fit // e.g. type(/asdf/g) --> "[object RegExp]" return Object.prototype.toString.call(x); } function zip(arrays) { // e.g. zip([[1,2,3],[4,5,6]]) --> [[1,4],[2,5],[3,6]] return arrays[0].map(function(_,i){ return arrays.map(function(array){return array[i]}) }); } // helper functions function allCompareEqual(array) { // e.g. allCompareEqual([2,2,2,2]) --> true // does not work with nested arrays or objects return array.every(function(x){return x==array[0]}); } function isArray(x){ return type(x)==type([]) } function getLength(x){ return x.length } function allTrue(array){ return array.reduce(function(a,b){return a&&b},true) } function allDeepEqual(things) { // works with nested arrays if( things.every(isArray) ) return allCompareEqual(things.map(getLength)) // all arrays of same length && allTrue(zip(things).map(allDeepEqual)); // elements recursively equal //else if( this.every(isObject) ) // return {all have exactly same keys, and for // each key k, allDeepEqual([o1[k],o2[k],...])} // e.g. ... && allTrue(objectZip(objects).map(allDeepEqual)) //else if( ... ) // extend some more else return allCompareEqual(things); } function allDeepEqual2() { return allDeepEqual([].slice.call(arguments)); } // ######################################################### // # https://stackoverflow.com/a/10316616/2601293 (Option 3) // ######################################################### function optionThree(a,b) { /* Array-aware equality checker: Returns whether arguments a and b are == to each other; however if they are equal-lengthed arrays, returns whether their elements are pairwise == to each other recursively under this definition. */ if (a instanceof Array && b instanceof Array) { if (a.length!=b.length) // assert same length return false; for(var i=0; i<a.length; i++) // assert each element equal if (!arraysEqual(a[i],b[i])) return false; return true; } else { return a==b; // if not both arrays, should be the same } } // ######################################################### // # https://stackoverflow.com/a/10316616/2601293 (Option 4) // ######################################################### function deepEquals(a,b) { if (a instanceof Array && b instanceof Array) return arraysEqual(a,b); if (Object.getPrototypeOf(a)===Object.prototype && Object.getPrototypeOf(b)===Object.prototype) return objectsEqual(a,b); if (a instanceof Map && b instanceof Map) return mapsEqual(a,b); if (a instanceof Set && b instanceof Set) throw "Error: set equality by hashing not implemented." if ((a instanceof ArrayBuffer || ArrayBuffer.isView(a)) && (b instanceof ArrayBuffer || ArrayBuffer.isView(b))) return typedArraysEqual(a,b); return a==b; // see note[1] -- IMPORTANT } function arraysEqual(a,b) { if (a.length!=b.length) return false; for(var i=0; i<a.length; i++) if (!deepEquals(a[i],b[i])) return false; return true; } function objectsEqual(a,b) { var aKeys = Object.getOwnPropertyNames(a); var bKeys = Object.getOwnPropertyNames(b); if (aKeys.length!=bKeys.length) return false; aKeys.sort(); bKeys.sort(); for(var i=0; i<aKeys.length; i++) if (aKeys[i]!=bKeys[i]) // keys must be strings return false; return deepEquals(aKeys.map(k=>a[k]), aKeys.map(k=>b[k])); } function mapsEqual(a,b) { if (a.size!=b.size) return false; var aPairs = Array.from(a); var bPairs = Array.from(b); aPairs.sort((x,y) => x[0]<y[0]); bPairs.sort((x,y) => x[0]<y[0]); for(var i=0; i<a.length; i++) if (!deepEquals(aPairs[i][0],bPairs[i][0]) || !deepEquals(aPairs[i][1],bPairs[i][1])) return false; return true; } function typedArraysEqual(a,b) { a = new Uint8Array(a); b = new Uint8Array(b); if (a.length != b.length) return false; for(var i=0; i<a.length; i++) if (a[i]!=b[i]) return false; return true; }
Tests:
lodash isEqual
_.isEqual(a, b)
arraysEqual
arraysEqual(a, b)
JSON.stringify (WONT WORK WITH OBJECTS)
arraysEqual(a, b)
Option 2
allDeepEqual2(a, b)
option 3
optionThree(a,b)
Option 4
deepEquals(a,b)