Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object comparison
(version: 0)
Comparing performance of:
lodash vs underscore vs JSON.stringify() JSON.st vs isEqualShallow()
Created:
9 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> <script> var underscore = _.noConflict(); </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.0/lodash.min.js"></script> <script> var lodash = _.noConflict(); </script> <script> Benchmark.prototype.setup = function() { var obj1 = { properties: { selectionName: null, price: { format: 'fractional', odds: null }, status: 'A', display: { displayed: 'Y', order: null }, selectionScore: { home: null, away: null }, band: { lower: 5, upper: 10 }, channels: 'ZWIP' }, result: null }; var obj2 = { properties: { selectionName: null, price: { format: 'fractional', odds: null }, status: 'A', display: { displayed: 'Y', order: null }, selectionScore: { home: null, away: null }, band: { lower: null, upper: null }, channels: 'ZWIP' }, result: null }; lodash = window.lodash; underscore = window.underscore window.isEqualShallow = function (a, b) { if (!a && !b) { return true; } if (!a && b || a && !b) { return false; } var numKeysA = 0, numKeysB = 0, key; for (key in b) { numKeysB++; if (/* !isPrimitive(b[key]) || */ !a.hasOwnProperty(key) || (a[key] !== b[key])) { // console.log(key, a, b); return false; } } for (key in a) { numKeysA++; } return numKeysA === numKeysB; } }; </script>
Tests:
lodash
window.lodash.isEqual(obj1, obj2);
underscore
window.underscore.isEqual(obj1, obj2);
JSON.stringify() JSON.st
JSON.stringify(obj1) == JSON.stringify(obj2);
isEqualShallow()
window.isEqualShallow(obj1, obj2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash
underscore
JSON.stringify() JSON.st
isEqualShallow()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
879226.5 Ops/sec
underscore
2809861.2 Ops/sec
JSON.stringify() JSON.st
937577.7 Ops/sec
isEqualShallow()
82496080.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark definition and test cases. **Benchmark Definition** The benchmark definition is represented by the JSON object: ``` { "Name": "Object comparison", "Description": null, "Script Preparation Code": null, "Html Preparation Code": ... } ``` This defines a general benchmark for comparing two objects, `obj1` and `obj2`. The script preparation code initializes the test data objects, including their properties. **Test Cases** The individual test cases are defined in the JSON array: ```javascript [ { "Benchmark Definition": "window.lodash.isEqual(obj1, obj2);", "Test Name": "lodash " }, { "Benchmark Definition": "window.underscore.isEqual(obj1, obj2);", "Test Name": "underscore " }, { "Benchmark Definition": "JSON.stringify(obj1) == JSON.stringify(obj2);", "Test Name": "JSON.stringify() JSON.st" }, { "Benchmark Definition": "window.isEqualShallow(obj1, obj2);", "Test Name": "isEqualShallow() " } ] ``` These test cases compare the two objects using different methods: 1. **Lodash**: `window.lodash.isEqual(obj1, obj2)`. This uses Lodash's `isEqual` function to compare the objects. 2. **Underscore**: `window.underscore.isEqual(obj1, obj2)`. This uses Underscore.js's `isEqual` function to compare the objects. 3. **JSON Stringification**: `JSON.stringify(obj1) == JSON.stringify(obj2)`. This compares the stringified representations of the two objects using the `==` operator. 4. **Custom `isEqualShallow` function**: `window.isEqualShallow(obj1, obj2)`. This uses a custom function to compare the objects, which is defined in the script preparation code. **Library and Its Purpose** Two libraries are used in this benchmark: 1. **Lodash**: Lodash is a popular JavaScript library that provides utility functions for tasks like array manipulation, object iteration, and more. In this benchmark, it's used to implement the `isEqual` function. 2. **Underscore**: Underscore.js is another popular JavaScript library that provides functional programming helpers. In this benchmark, it's used to implement the `isEqual` function. **Special JS Feature or Syntax** None of the test cases explicitly use any special JavaScript features or syntax. **Other Considerations** When choosing a method for comparing objects, there are several factors to consider: * **Deep equality**: Do you need to compare the object's properties recursively? Or can you assume that some properties will not be present in both objects? * **Type checking**: Do you need to ensure that the types of the object's properties match between the two objects? * **Performance**: Different comparison methods have different performance characteristics. For example, stringification can be slower than using a library like Lodash. In this benchmark, all four test cases are designed to demonstrate different approaches to comparing objects. The choice of method will depend on the specific requirements of your use case.
Related benchmarks:
Compare comparison with null or undefined
Lodash values vs Object.values
object deep comparison
Compare comparison with null or undefined. With typeof
Compare comparison with null or undefined (updated)
Comments
Confirm delete:
Do you really want to delete benchmark?