I have two arrays. In each array I have objects with lots of properties but no methods. I need to see if array 1 is equal with array 2.
One way to do that would be to create a function that pass through each element of an array and pare each property of the object with the object in the similar position in the second array.
The problem is that the arrays are quite big and also each object has lots of properties. I was wandering if there could be another way. In C++ for example I could read memory... but I don't know how to do that in js.
I need to obtain the most optimal way since this is part of a function that is used often.
I have two arrays. In each array I have objects with lots of properties but no methods. I need to see if array 1 is equal with array 2.
One way to do that would be to create a function that pass through each element of an array and pare each property of the object with the object in the similar position in the second array.
The problem is that the arrays are quite big and also each object has lots of properties. I was wandering if there could be another way. In C++ for example I could read memory... but I don't know how to do that in js.
I need to obtain the most optimal way since this is part of a function that is used often.
Share Improve this question edited Jun 9, 2014 at 11:21 zozo asked Feb 8, 2012 at 10:46 zozozozo 8,61219 gold badges83 silver badges141 bronze badges 1- Compare javascript array of array by distinct values this code can help you out. – Jugal Commented Sep 21, 2013 at 7:12
3 Answers
Reset to default 4Unless they are the same array instance, paring the memory locations won't work in JavaScript (what happens when you do arr1 == arr2
).
You would need to explicitly loop.
Some people use JSON.stringify()
(watch out for the gotcha explained in the ments by pimvdb) on both arrays and pare the resulting strings to cheat, but serialising to a string and paring sounds over fully expensive to me. However it works, so if there is no performance problem, go nuts! :)
You could also try toSource()
.
I would build my own parative function that pares just enough that satisfies my idea of identical.
Converting your arrays to strings and then paring the strings will have the same average and worst performance: O(n) (linear).
If you loop through your objects properties/arrays and abort on the 1st mismatch your worst performance will still be O(n) but your average performance might significantly improve unless the objects your'e paring are usually identical. Either way, since this traversal wouldn't include creating any new objects and copying bytes around - even paring identical posite objects/arrays (worst case) should still be faster than stringifying them.
As this answer suggests you could just use Underscore.js isEqual:
which according to docs: Performs an optimized deep parison between the two objects, to determine if they should be considered equal
I'm pretty sure it will work for arrays too.
JQuery has a function called jQuery.param() which serializes objects
You can pare objects or arrays of objects like so,
$.param( originalObj ) == $.param( modifiedObj )
It's very powerful in conjuction with jQuery.extend() which can be used to clone objects