Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
withoutObjectKeys again
(version: 0)
Comparing performance of:
reduce vs clone & delete vs foreach
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 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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce
clone & delete
foreach
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 provided benchmark and explain what is being tested. **Benchmark Description** The benchmark is testing three different approaches to remove object keys from an object based on a blacklist. The test case uses the `withoutObjectKeys` function, which is defined in the "Script Preparation Code" section of the benchmark definition JSON. The function takes two arguments: `blacklist` and `object`. It returns a new object that only includes the key-value pairs where the key is not present in the `blacklist`. **Options Compared** The three options being compared are: 1. `withoutObjectKeys`: This approach uses the `reduce()` method to iterate over the keys of the original object and filter out the ones present in the `blacklist`. 2. `withoutObjectKeys2`: This approach creates a copy of the original object using the spread operator (`{...object}`), and then deletes the key-value pairs that are present in the `blacklist` by calling the `delete` keyword. 3. `withoutObjectKeys3`: This approach creates an empty object (`const clone = {}`) and then iterates over the keys of the original object, assigning the value to the cloned object only if the key is not present in the `blacklist`. **Pros and Cons** Here are some pros and cons of each approach: 1. **withoutObjectKeys`: * Pros: Efficient use of `reduce()` method, which can be optimized for performance. * Cons: May have higher memory overhead due to creating a new object with filtered key-value pairs. 2. **withoutObjectKeys2`: * Pros: Simple and easy to understand, as it uses the spread operator and `delete` keyword. * Cons: May be slower than `withoutObjectKeys` due to the creation of a copy of the original object and subsequent deletion of key-value pairs. 3. **withoutObjectKeys3`: * Pros: Avoids creating a new object with filtered key-value pairs, which can reduce memory overhead. * Cons: May be less efficient than `withoutObjectKeys` due to the need for explicit iteration over keys. **Libraries and Special JS Features** There are no libraries used in this benchmark. However, some JavaScript features are being utilized: * The spread operator (`{...object}`) is used in `withoutObjectKeys2` to create a copy of the original object. * The `delete` keyword is used in `withoutObjectKeys2` to delete key-value pairs from the cloned object. **Alternatives** Other approaches to remove object keys based on a blacklist could include: 1. Using `Object.fromEntries()` and filtering out key-value pairs using an array filter. 2. Using a library like Lodash's `omitBy()` function, which is optimized for performance. 3. Using a more functional programming approach with `reduce()` or `map()` methods. Keep in mind that the choice of approach depends on the specific requirements and constraints of the use case.
Related benchmarks:
withoutObjectKeys
withoutObjectKeys again and again
Lodash omit vs es omit vs compiled omit vs rest and delete omit
Lodash omit vs es omit vs compiled omit vs rest and delete omit vs babel - a lot of different objects
Comments
Confirm delete:
Do you really want to delete benchmark?