Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object reduction with conversion back (smol)
(version: 0)
Comparing performance of:
A vs Spread/delete
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = { ...Array.from(Array(500).keys()) }; var idsToRemove = [1, 47, 420, 421, 69, 500, 87, 238];
Tests:
A
Object.fromEntries(Object.entries(data).filter(([k, v]) => { !idsToRemove.includes(k); }));
Spread/delete
var newObjs = { ...data }; idsToRemove.forEach((id) => { delete newObjs[id]; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
A
Spread/delete
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
A
78067.5 Ops/sec
Spread/delete
79596.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The benchmark is designed to test the performance of JavaScript in reducing an object while removing certain properties. The benchmark uses two different approaches: 1. **A**: Uses `Object.fromEntries` to create a new object from the remaining key-value pairs. 2. **Spread/delete**: Uses the spread operator (`...`) to create a shallow copy of the original object, and then deletes specific properties using `delete`. **Library Used:** In both test cases, the benchmark uses the JavaScript built-in `Array.from` method to generate an array of keys (0-499), which is used to create the initial object. No external libraries are required. **Special JS Feature/Syntax:** Neither test case uses any special JavaScript features or syntax that's not widely supported. Both approaches rely on standard JavaScript methods (`Object.entries`, `filter`, `delete`). **Options Compared:** The benchmark compares two different approaches to reduce an object while removing certain properties: 1. **A**: Uses `Object.fromEntries` 2. **Spread/delete**: Uses the spread operator and `delete` **Pros and Cons:** **A (Object.fromEntries)** Pros: * Creates a new object from the remaining key-value pairs, which can be more efficient than modifying the original object. * Can be more predictable and easier to reason about. Cons: * May incur additional overhead due to the creation of a new object. * Can lead to unnecessary memory allocations if not implemented correctly. **Spread/delete** Pros: * Modifies the original object directly, which can reduce memory allocation overhead. * Can be faster in some cases since it doesn't create an intermediate object. Cons: * May lead to unexpected behavior or side effects if not used carefully (e.g., modifying the original object). * Can make code harder to read and maintain due to the use of `delete`. **Other Considerations:** * **Object literals vs. objects with methods**: If the initial object has methods, using `Object.fromEntries` may lead to unexpected behavior or errors. * **Performance benefits**: In some cases, the spread operator might be faster than creating a new object using `Object.fromEntries`. However, this can depend on various factors, such as the JavaScript engine and the specific use case. **Alternatives:** Other approaches to reduce an object while removing certain properties include: 1. **Using a library like Lodash**: Functions like `_omit` or `_differenceBy` can simplify the process. 2. **Implementing a custom reduction function**: Using a recursive or iterative approach to remove specific properties from an object. 3. **Using `Object.assign()` and filtering**: Combining `Object.assign()` with array methods (e.g., `filter()`) to create a new object. Keep in mind that the choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
Object.fromEntries vs reduce - convert Array to Object
Map convert
Object.fromEntries vs reduce round 2
Object reduction with conversion back
Comments
Confirm delete:
Do you really want to delete benchmark?