Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object filter mapping
(version: 0)
benchmark comparing the perf of creating a new object by filter out entries
Comparing performance of:
Object.entries vs Object.key
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } window.parentObj = {}; for (let i = 0; i < 100; i++) { window.parentObj[makeid()] = makeid(); }
Tests:
Object.entries
Object.fromEntries(Object.entries(window.parentObj).filter(([k, v], i) => ((i % 2) === 0)));
Object.key
Object.keys(window.parentObj).reduce((acc, k, i) => { if ((i % 2) === 0) { acc[k] = window.parentObj[k]; } return acc }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.entries
Object.key
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:126.0) Gecko/20100101 Firefox/126.0
Browser/OS:
Firefox 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries
278363.6 Ops/sec
Object.key
199917.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of creating an object by filtering out entries using `Object.fromEntries`, `Object.entries`, and `Object.keys` can be done in several ways, each with its own pros and cons. **Approach 1: Using Object.fromEntries** `Object.fromEntries` is a relatively new method introduced in JavaScript ES2020. It takes an iterable of key-value pairs and converts it into an object. ```javascript Object.fromEntries([ ['key1', 'value1'], ['key2', 'value2'] ]); ``` Pros: * Efficient and concise way to create objects. * Easy to read and maintain. Cons: * Only supported in modern browsers (Firefox 126+). * May not be compatible with older JavaScript engines or environments. **Approach 2: Using Object.entries** `Object.entries()` returns an array of key-value pairs for an object. ```javascript Object.entries({key1: 'value1', key2: 'value2'}); ``` Pros: * Widespread browser support (including older versions). * Easy to use and understand. Cons: * May be less efficient than `Object.fromEntries` due to the overhead of creating an array. * Requires additional processing to convert the array into an object. **Approach 3: Using Object.keys** `Object.keys()` returns an array of keys for an object. ```javascript Object.keys({key1: 'value1', key2: 'value2'}); ``` Pros: * Widespread browser support (including older versions). * Easy to use and understand. Cons: * May be less efficient than `Object.fromEntries` due to the overhead of creating an array. * Requires additional processing to filter out odd-indexed keys. **Library:** The benchmark script uses a custom library (`makeid`) to generate random strings. The purpose of this library is to create unique identifiers for the object entries, allowing the benchmark to focus on the filtering and mapping process. **Special JS Feature/Syntax:** There is no special JavaScript feature or syntax used in this benchmark. Now, let's compare these approaches: * `Object.fromEntries` is likely to be the fastest option due to its built-in support for creating objects from iterables. * `Object.entries` may be slightly slower than `Object.fromEntries`, but it provides a more traditional way of accessing object entries. * `Object.keys` is likely to be the slowest option, as it requires additional processing to filter out odd-indexed keys. Alternative approaches: * Using a library like Lodash or Ramda to implement filtering and mapping functions can provide additional performance benefits. * Using WebAssembly (WASM) or other native modules can also provide performance advantages. * Using alternative object creation methods, such as `Object.create()` or `Array.prototype.reduce()`, may also be worth exploring. Keep in mind that these are general observations, and the actual performance differences between these approaches will depend on specific use cases and environment configurations.
Related benchmarks:
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Object values: Object.entries loop for
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS for .. of
Object filter mappingv2
Object filter mappingv3
Comments
Confirm delete:
Do you really want to delete benchmark?