Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
redasdfasdf
(version: 0)
Comparing performance of:
for vs map vs reduce
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [...Array(10000)].reduce((acc, _, i) => { acc[i.toString()] = i; return acc; }, {}); var keys = [...Array(5000)].map((_, i) => i.toString()); function pickFor(obj, keys) { const ret = {}; for (const key of keys) { ret[key] = obj[key]; } return ret; } function pickMap(obj, keys) { const entries = keys.map((key) => [key, obj[key]]); return Object.fromEntries(entries); } function pickReduce(obj, keys) { return keys.reduce((acc, i) => { acc[i] = obj[i]; return acc; }, {}); }
Tests:
for
pickFor(data, keys);
map
pickMap(data, keys);
reduce
pickReduce(data, keys);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
map
reduce
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 Definition JSON** The benchmark definition JSON represents three JavaScript functions: `pickFor`, `pickMap`, and `pickReduce`. These functions are used to extract specific properties from an object based on a given array of keys. The test script prepares two arrays, `data` and `keys`, which are used as input for these functions. **Functions Being Tested** 1. **pickFor**: This function iterates over the `keys` array and extracts the corresponding property from the `data` object using bracket notation (`obj[key]`). It then returns an object with the extracted properties. 2. **pickMap**: Similar to `pickFor`, this function uses the `map()` method to create an array of key-value pairs, where each pair consists of a key from the `keys` array and the corresponding property from the `data` object. The resulting array is then converted to an object using `Object.fromEntries()`. 3. **pickReduce**: This function uses the `reduce()` method to iterate over the `keys` array and extract the corresponding properties from the `data` object. It returns an object with the extracted properties. **Options Compared** The benchmark compares the performance of each function (`pickFor`, `pickMap`, and `pickReduce`) using the same input data (`data` and `keys` arrays). **Pros and Cons of Each Approach** 1. **pickFor**: This approach is simple and uses traditional JavaScript methods (bracket notation, loops). It's easy to understand and implement. * Pros: Easy to understand, fast execution. * Cons: May have performance issues if the input data is large or complex. 2. **pickMap**: This approach uses a more modern JavaScript method (`map()` and `Object.fromEntries()`) which can be faster than traditional loops. * Pros: Faster execution, concise code. * Cons: May not perform well on very large inputs or in older browsers. 3. **pickReduce**: This approach uses the `reduce()` method, which can be slower than other methods due to its overhead. * Pros: Can handle complex data structures, easy to implement. * Cons: Slower execution compared to `pickFor` and `pickMap`. **Library Used** None of the functions use any external libraries. **Special JS Features or Syntax** The benchmark uses modern JavaScript features such as: * `map()` * `Object.fromEntries()` * `reduce()` These features are supported by most modern browsers, including Chrome 112. **Other Alternatives** If you need to extract properties from an object based on a given array of keys, consider using the following alternatives: * **Lodash**: A popular JavaScript library that provides a `pick()` function for extracting properties. * **Underscore.js**: Another popular JavaScript library that provides a `pick` function for extracting properties. * **Manual implementation**: You can also implement a custom solution using traditional loops and bracket notation. Note that the benchmark's focus is on comparing the performance of these three specific functions, so it's essential to choose an alternative that matches your use case.
Related benchmarks:
Reduce vs Map+FromEntries vs for loop
Object.fromEntries vs reduce vs Map + Array.from
Reduce vs Map+FromEntries vs for loop vs forEach
Benchmark: flatMap vs reduce vs while vs foreach (40k)
Comments
Confirm delete:
Do you really want to delete benchmark?