Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
withoutObjectKeys again and again
(version: 0)
Comparing performance of:
reduce vs clone & delete vs foreach vs for in, indexOf
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var withoutObjectKeys = function (blacklist, object) { return Object.keys(object).reduce(function (acc, key) { return blacklist.includes(key) ? acc : {...acc, [key]: object[key]}; }, {}); }; var withoutObjectKeys2 = function (blacklist, object) { const clone = {...object}; for (var key of blacklist) { delete clone[key]; } }; var withoutObjectKeys3 = function (blacklist, object) { const clone = {}; for (var key of Object.keys(object)) { if (!blacklist.includes(key)) { clone[key] = object[key]; } } }; var withoutObjectKeys4 = function (blacklist, object) { const clone = {}; for (var key in object) { if ( Object.prototype.hasOwnProperty.call(object, key) && blacklist.indexOf(key) === -1 ) { clone[key] = object[key]; } } }; var original = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 8}; var keys = ['a', 'c', 'e', 'g']
Tests:
reduce
withoutObjectKeys(keys, original)
clone & delete
withoutObjectKeys2(keys, original)
foreach
withoutObjectKeys3(keys, original)
for in, indexOf
withoutObjectKeys4(keys, original)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
reduce
clone & delete
foreach
for in, indexOf
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):
Measuring the performance of different approaches to remove keys from an object can be a valuable exercise for JavaScript developers. **Benchmark Definition** The benchmark defines four functions to remove keys from an object: 1. `withoutObjectKeys`: uses `Object.keys()` and `reduce()` 2. `withoutObjectKeys2`: creates a clone of the original object using the spread operator (`{...object}`) and then deletes specific keys 3. `withoutObjectKeys3`: creates a new object and iterates over the keys of the original object, adding properties only if not in the blacklist 4. `withoutObjectKeys4`: uses `in` operator to check for own property existence and `indexOf()` method to find index of key in the blacklist **Comparison Options** The four functions differ in their approach: * **Memory allocation**: `withoutObjectKeys2` creates a clone of the original object, while the other three functions modify the existing object. This may have implications for memory usage and garbage collection. * **Iteration style**: `withoutObjectKeys3` uses a traditional `for...in` loop, whereas `withoutObjectKeys4` relies on the `in` operator to check for own properties. * **Blacklist search**: `withoutObjectKeys2` searches for key presence in the blacklist using `indexOf()`, while `withoutObjectKeys4` checks if the key is present in the blacklist using `indexOf()`. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **withoutObjectKeys**: uses `reduce()` which can be memory-efficient but may have performance implications due to function call overhead. 2. **withoutObjectKeys2**: creates a clone, which might lead to slower performance due to memory allocation, but avoids modifying the original object. 3. **withoutObjectKeys3**: traditional `for...in` loop is straightforward but might be slower than other approaches due to iteration overhead. 4. **withoutObjectKeys4**: uses `in` operator, which can be more readable and efficient than manual checks for key presence. **Library Use** None of the functions rely on external libraries. However, `withoutObjectKeys2` relies on modern JavaScript features like the spread operator (`{...object}`) and destructuring assignment (not explicitly used but implied). **Special JS Features or Syntax** No special JS features are explicitly used in this benchmark. **Other Alternatives** Alternative approaches to remove keys from an object might include: * Using `Object.fromEntries()` and `Array.from()` with a filter function * Creating a new array of allowed keys and then using `reduce()` or other iteration methods * Utilizing libraries like Lodash or Ramda for functional programming utilities Keep in mind that the specific optimization choice ultimately depends on the requirements of your application, including factors like performance, code readability, and maintainability.
Related benchmarks:
withoutObjectKeys
withoutObjectKeys again
Lodash omit vs es omit vs compiled omit vs rest and delete omit
Cloning b81d8fae-190a-4c8b-a9af-08bebc52bf2a
Comments
Confirm delete:
Do you really want to delete benchmark?