Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique-props
(version: 0)
Comparing performance of:
unique - for...in vs unique - keys.reduce vs unique - set + reduce vs unique - set + for...in
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getUniqueProps0(objA = {}, objB = {}) { const diff = {}; for (const key in objB) { if (!(key in objA)) { diff[key] = objB[key] } } return diff; } function getUniqueProps1(objA = {}, objB = {}) { return Object.keys(objB).reduce((diff, key) => { if (!(key in objA)) { diff[key] = objB[key]; } return diff; }, {}); } function getUniqueProps2(objA = {}, objB = {}) { const keySet = new Set(Object.keys(objA)); return Object.keys(objB).reduce((diff, key) => { if (!keySet.has(key)) { diff[key] = objB[key]; } return diff; }, {}); } function getUniqueProps3(objA = {}, objB = {}) { const keySet = new Set(Object.keys(objA)); const diff = {}; for (const key in objB) { if (!keySet.has(key)) { diff[key] = objB[key]; } } return diff; }
Tests:
unique - for...in
const metaProps = { 'a': 1, 'b': 1, 'c': 1 }; const props = { 'b': 1, 'c': 1, 'd': 1 }; getUniqueProps0(metaProps, props);
unique - keys.reduce
const metaProps = { 'a': 1, 'b': 1, 'c': 1 }; const props = { 'b': 1, 'c': 1, 'd': 1 }; getUniqueProps1(metaProps, props);
unique - set + reduce
const metaProps = { 'a': 1, 'b': 1, 'c': 1 }; const props = { 'b': 1, 'c': 1, 'd': 1 }; getUniqueProps2(metaProps, props);
unique - set + for...in
const metaProps = { 'a': 1, 'b': 1, 'c': 1 }; const props = { 'b': 1, 'c': 1, 'd': 1 }; getUniqueProps3(metaProps, props);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
unique - for...in
unique - keys.reduce
unique - set + reduce
unique - set + for...in
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of four different approaches to find unique properties between two objects, `objA` and `objB`. The benchmark uses JavaScript functions: `getUniqueProps0`, `getUniqueProps1`, `getUniqueProps2`, and `getUniqueProps3`. **Approaches Compared** Here's a brief description of each approach: 1. **`getUniqueProps0`**: This function iterates over the keys of `objB` and checks if the key is present in `objA`. If not, it adds the key-value pair to an object `diff`. 2. **`getUniqueProps1`**: This function uses the `Object.keys()` method to get an array of keys from `objB`, and then uses the `reduce()` method to iterate over the array and add unique key-value pairs to an object `diff`. 3. **`getUniqueProps2`**: This function creates a Set of keys from `objA` using `Set(Object.keys(objA))`. Then, it iterates over the keys of `objB` using `Object.keys()` and checks if each key is present in the Set. If not, it adds the key-value pair to an object `diff`. 4. **`getUniqueProps3`**: This function creates a Set of keys from `objA` using `Set(Object.keys(objA))`. Then, it iterates over the keys of `objB` using a traditional `for...in` loop and checks if each key is present in the Set. If not, it adds the key-value pair to an object `diff`. **Pros and Cons** Here are some pros and cons for each approach: * **`getUniqueProps0`**: + Pros: Simple and easy to understand. + Cons: May be slower due to the iterative approach. * **`getUniqueProps1`**: + Pros: Uses `reduce()`, which can be faster than a traditional loop. + Cons: Requires understanding of the `reduce()` method, which may make it less accessible to beginners. * **`getUniqueProps2`**: + Pros: Uses a Set, which can provide efficient lookups. + Cons: May require more memory due to the Set creation. * **`getUniqueProps3`**: + Pros: Uses a traditional `for...in` loop, which may be familiar to some developers. + Cons: May be slower than the other approaches due to the iterative approach. **Library/Functionality Used** None of the functions used in this benchmark rely on external libraries or advanced JavaScript features. They are all standard JavaScript methods and functions. **Special JS Features** While not using any external libraries, these functions do utilize some advanced JavaScript concepts, such as: * `Set` objects (in `getUniqueProps2`) * The `Object.keys()` method (used in all four approaches) * The `reduce()` method (used in `getUniqueProps1`) * Traditional `for...in` loops (used in `getUniqueProps3`) **Benchmark Performance** The benchmark shows the performance of each approach, with Safari 13 running on a Mac OS X 10.14.6 machine. The results indicate that: * `getUniqueProps0` is the slowest * `getUniqueProps1` and `getUniqueProps2` are comparable in terms of speed * `getUniqueProps3` is the fastest Overall, this benchmark provides a useful comparison of different approaches to finding unique properties between two objects in JavaScript.
Related benchmarks:
diffcd
comparing two equal checkers function
equality objects functions
hasOwnProperty vs Object.keys to check whether an Object is empty
Comments
Confirm delete:
Do you really want to delete benchmark?