Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
dissoc & dissoc2
(version: 2)
Comparing performance of:
dissoc vs dissoc2 vs dissoc3
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function dissoc(map, ...keys) { const newMap = Object.keys(map).reduce((accu, key) => { if (keys.indexOf(key) < 0) { accu[key] = map[key] return accu } return accu }, {}) return newMap } function dissoc2(map, ...keys) { const newMap = Object.assign({}, map) for (const k of keys) { delete newMap[k] } return newMap } function dissoc3(map, ...keys) { const newMap = Object.assign({}, map) keys.forEach((k,i) => { delete newMap[k] }) return newMap }
Tests:
dissoc
dissoc({a:1, b:2, c:3, d:4}, 'a','d')
dissoc2
dissoc2({a:1, b:2, c:3, d:4}, 'a','d')
dissoc3
dissoc3({a:1, b:2, c:3, d:4}, 'a','d')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
dissoc
dissoc2
dissoc3
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):
**Benchmark Overview** The MeasureThat.net benchmark is designed to measure the performance of different approaches to removing keys from an object in JavaScript. The benchmark consists of three test cases: `dissoc`, `dissoc2`, and `dissoc3`. Each test case uses a different implementation to remove keys from an object. **Benchmark Definitions** The three benchmark definitions are: 1. `dissoc(map, ...keys)`: This function takes a map (object) and one or more keys as arguments. It returns a new map with only the key-value pairs that are not in the list of provided keys. 2. `dissoc2(map, ...keys)`: This function takes a map and one or more keys as arguments. It creates a copy of the original map using `Object.assign()`, then deletes each key from the copy. 3. `dissoc3(map, ...keys)`: This function is similar to `dissoc2`, but it uses a `forEach` loop to delete each key from the copy. **Comparison of Options** The three options are compared in terms of their performance: * **`dissoc`**: Uses `Object.keys()` and `reduce()` to iterate over the keys of the original map. This approach is likely to be slower than the other two options because it requires iterating over all key-value pairs. * **`dissoc2`**: Creates a copy of the original map using `Object.assign()`, then deletes each key from the copy using `delete`. This approach is faster than `dissoc` but may use more memory because it creates an additional copy of the object. * **`dissoc3`**: Uses a `forEach` loop to delete each key from the copy, which makes it slightly slower than `dissoc2`. **Pros and Cons** Here are some pros and cons of each approach: * **`dissoc`**: + Pros: Simple and easy to understand. + Cons: May be slower due to iterating over all key-value pairs. * **`dissoc2`**: + Pros: Faster than `dissoc` and uses less memory because it creates only one copy of the object. + Cons: May use more CPU cycles due to the `Object.assign()` call. * **`dissoc3`**: + Pros: Similar performance to `dissoc2`. + Cons: Uses a `forEach` loop, which may be slightly slower. **Library and Syntax** The benchmark uses no external libraries or special JavaScript features. The implementations are straightforward and easy to understand. **Other Considerations** When choosing an approach for removing keys from an object in JavaScript, consider the following factors: * Performance: If speed is critical, `dissoc2` or `dissoc3` may be a better choice. * Memory usage: If memory efficiency is important, `dissoc2` or `dissoc3` may be a better choice. * Code readability: If code readability is more important than performance, `dissoc` may be a better choice. **Alternatives** Some alternative approaches to removing keys from an object in JavaScript include: * Using a library like Lodash or Underscore.js, which provide optimized implementations for this operation. * Using the `delete` operator with a `for...in` loop to iterate over the keys of the object. * Using the `Object.keys()` method and a `forEach` loop to remove keys from an object.
Related benchmarks:
dissoc
Object.fromEntries vs reduce v3
Object vs Map creation (forEach, reduce)
Object Keys vs Entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?