Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
dissoc
(version: 0)
Comparing performance of:
dissoc vs dissoc2
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function dissoc(map, ...keys) { return Object.keys(map).reduce((accu, key) => { if (keys.indexOf(key) < 0) { return accu } accu[key] = map[key] return accu }, {}) } function dissoc2(map, ...keys) { const newMap = Object.assign({}, map) for (const k of keys) { delete newMap[k] } return newMap }
Tests:
dissoc
dissoc({a: 1, b:2, c:3, d:4}, 'a', 'c')
dissoc2
dissoc2({a: 1, b:2, c:3, d:4}, 'a', 'c')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
dissoc
dissoc2
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 definition and test cases. **Benchmark Definition** The benchmark definition is a JSON object that represents a JavaScript function, `dissoc`. The function takes two arguments: an object `map` and a variable number of keys (`...keys`). The function returns a new object with only the specified keys removed from the original object. **Script Preparation Code** The script preparation code consists of two functions: 1. `dissoc(map, ...keys)`: This function uses the `Object.keys()` method to get an array of the object's key names, and then iterates over this array using `reduce()`. For each key that is not in the `keys` array, it simply returns the current accumulator value (i.e., an empty object). If a key is found in the `keys` array, it adds the key-value pair to the new object. 2. `dissoc2(map, ...keys)`: This function uses `Object.assign()` to create a shallow copy of the original object (`map`). It then iterates over the `keys` array and deletes each key from the copied object using `delete`. **Html Preparation Code** The html preparation code is empty, which means that no HTML page is prepared for running the benchmark. **Individual Test Cases** There are two test cases: 1. `dissoc`: This test case uses the first function (`dissoc(map, ...keys)`) to remove the specified keys from an object. 2. `dissoc2`: This test case uses the second function (`dissoc2(map, ...keys)`) to achieve the same result as the first test case. **Options Compared** The two functions use different approaches to achieve the same result: 1. `dissoc(map, ...keys)`: * Uses `Object.keys()` and `reduce()` to iterate over the object's keys. * Returns a new object with only the specified keys removed. * Pros: Can handle objects of arbitrary complexity, can be used for other purposes beyond just removing keys. * Cons: May have higher overhead due to the use of `reduce()` and creating a new object. 2. `dissoc2(map, ...keys)`: * Uses `Object.assign()` to create a shallow copy of the original object. * Deletes each key from the copied object using `delete`. * Pros: May have lower overhead due to the use of `Object.assign()` and `delete`, can be faster for large objects. * Cons: Can only handle shallow copies, may not work well with complex objects. **Pros and Cons** Both functions have their pros and cons. The first function (`dissoc(map, ...keys)`) is more versatile but may have higher overhead due to the use of `reduce()` and creating a new object. The second function (`dissoc2(map, ...keys)`) is faster for large objects but can only handle shallow copies. **Special JS Features or Syntax** Neither test case uses any special JavaScript features or syntax beyond standard language support. **Alternatives** Other alternatives to achieve the same result could be: 1. Using `Object.fromEntries()` and `Object.entries()` to iterate over the object's key-value pairs. 2. Using a library like Lodash, which provides a `difference` function for removing keys from an object. 3. Using a different approach altogether, such as using a hash map or a data structure specifically designed for set operations. However, these alternatives may not be as efficient or suitable for all use cases as the two provided functions.
Related benchmarks:
dissoc & dissoc2
Reduce Object.assign vs spread
Object Keys vs Entries 2
map-values
Comments
Confirm delete:
Do you really want to delete benchmark?