Array comparison in plain JavaScript
Comparing two arrays in JavaScript :
Array.prototype.isEqualTo = function ( compareTo ) {
if ( this.length !== compareTo.length ) return false;
for ( var i = 0; i < this.length; i ++ ){
if ( this[i] !== compareTo[i] ) return false;
}
return true;
}
Written by Arne Fostvedt
Related protips
1 Response
With a few performance improvements :
Array.prototype.isEqualTo = function (compareTo) {
var self = this // cache array
, length = self.length // cache length
if(length != compareTo.length) return false // compare numbers, strict equalty isn't necessary
for(;length--;) if(self[length] !== compareTo[length]) return false // decrement is faster with for loops
return true
}
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Array
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#