Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
withoutObjectKeys
(version: 0)
Comparing performance of:
reduce vs clone & delete
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 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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
clone & delete
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's being tested. **Benchmark Overview** The benchmark compares two approaches to remove object keys from an object in JavaScript: 1. `withoutObjectKeys` (using `Object.keys()` and `reduce()`) 2. `withoutObjectKeys2` (using a clone of the original object and deleting specific keys) **What are we testing?** We're testing how fast each approach is when removing object keys, specifically for a given set of blacklisted keys (`keys`) from an original object (`original`). **Options Compared** The two options being compared are: 1. `withoutObjectKeys`: uses the `Object.keys()` method to get an array of object keys and then uses the `reduce()` method to iterate over the keys, checking if each key is in the blacklist. If it's not, the value is added to a new object. 2. `withoutObjectKeys2`: creates a clone of the original object using the spread operator (`{...object}`) and then iterates over the blacklisted keys, deleting each corresponding key from the clone. **Pros and Cons** Here are some pros and cons for each approach: 1. `withoutObjectKeys`: * Pros: concise and readable code, uses a built-in method (`Object.keys()`), can be more efficient since it avoids unnecessary cloning. * Cons: may be slower due to the use of `reduce()`, which is a more complex operation than simply deleting keys from an object. 2. `withoutObjectKeys2`: * Pros: faster and more straightforward, uses a simple loop to delete keys, can avoid the overhead of built-in methods like `reduce()` or `Object.keys()`. * Cons: requires creating a clone of the original object, which may be slower for large objects. **Library and Syntax** In this benchmark, the `Object.keys()` method is used to get an array of object keys. This is a built-in JavaScript method that returns an array of strings representing the enumerable property names of an object. There are no special JS features or syntax being tested in this benchmark. The code uses standard JavaScript concepts like loops, objects, and function literals. **Alternative Approaches** Here are some alternative approaches to removing object keys: 1. Using a `for...in` loop: instead of using `Object.keys()`, you could use a `for...in` loop to iterate over the object's own enumerable properties. ```javascript function withoutObjectKeys3(blacklist, object) { const result = {}; for (const key in object) { if (!blacklist.includes(key)) { result[key] = object[key]; } } return result; } ``` 2. Using `Object.fromEntries()`: you could use `Object.fromEntries()` to create a new object from an array of key-value pairs, and then filter out the blacklisted keys. ```javascript function withoutObjectKeys4(blacklist, object) { const entries = Object.entries(object); return Object.fromEntries(entries.filter(([key]) => !blacklist.includes(key))); } ``` These alternative approaches may have different performance characteristics compared to `withoutObjectKeys` and `withoutObjectKeys2`.
Related benchmarks:
withoutObjectKeys again
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?