<script src="https://cdn.jsdelivr.net/npm/es-toolkit@%5E1"></script>
const node1 = {
id: "n-w71cvCJS6w7WM8E3C3_6w",
order: "a06DJ",
type: "shape",
attributes: {
width: 100,
height: 100,
color: '#fff',
}
};
const node2 = {
id: "n-w71cvCJS6w7WM8E3C3_6w",
order: "a06DJ",
type: "shape",
attributes: {
width: 100,
height: 100,
color: '#fff',
}
};
_.isEqual(node1, node2);
JSON.stringify(node1) === JSON.stringify(node2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
es-toolkit | |
JSON.stringify |
Test name | Executions per second |
---|---|
es-toolkit | 2184983.8 Ops/sec |
JSON.stringify | 3298864.8 Ops/sec |
The benchmark provided compares two methods of determining if two JavaScript objects (node1
and node2
) are equal in value. The two different approaches are evaluated to see which is more performant.
es-toolkit's _.isEqual
Method:
es-toolkit
library, which is a utility library designed to provide various utility functions for working with JavaScript data structures. The isEqual
function checks for deep equality between two values, meaning it will traverse objects and arrays to determine if they are equivalent in terms of structure and content.JSON.stringify Method:
JSON.stringify
to convert both node1
and node2
into JSON string representations, then compares the resulting strings for equality. This method works well for simple structures that can be serialized into JSON format.The benchmark results show that JSON.stringify
outperforms es-toolkit
's _.isEqual
. Specifically:
JSON.stringify
: 3,298,864.75 executions per second.es-toolkit
: 2,184,983.75 executions per second.This indicates that, at least for the given test cases and the structures provided, the stringification method offers better performance.
Edge Cases: Developers should consider the types of data they are comparing. If the comparison needs to handle complex objects (objects with methods, circular references, etc.), the es-toolkit
function is more appropriate despite its performance cost.
Alternatives to Consider:
_.isEqual
: Another utility library, Lodash, has a similar isEqual
function that many developers use. Results may vary by implementation, so testing would be needed.This evaluation can help developers decide on the appropriate method of value comparison in their applications based on performance considerations and the nature of the data being handled.