Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce vs map vs for
(version: 0)
Comparing performance of:
reduce vs map vs light vs map2 vs light 2
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="//raw.githubusercontent.com/lodash/lodash/4.17.15-npm/lodash.min.js"></script>
Script Preparation code:
var fieldsMap = { 'array[0].sarray[0]': 'array[1].sarray[1]', 'array[0].sarray[1].ssarray[0].sssarray[0]': 'array[1].sarray[3].ssarray[0].sssarray[1]', 'array[0].sarray[1].ssarray[1].sssarray[0]': 'array[1].sarray[3].ssarray[1].sssarray[1]', 'array[0].sarray[1].ssarray[2].sssarray[0]': 'array[1].sarray[3].ssarray[2].sssarray[1]', 'array[0].sarray[1].ssarray[3].sssarray[0]': 'array[1].sarray[3].ssarray[3].sssarray[1]', 'array[0].sarray[1].ssarray[4].sssarray[0]': 'array[1].sarray[3].ssarray[4].sssarray[1]', 'array[0].sarray[1].ssarray[5].sssarray[0]': 'array[1].sarray[3].ssarray[5].sssarray[1]', 'array[0].sarray[1].ssarray[6].sssarray[0]': 'array[1].sarray[3].ssarray[6].sssarray[1]', 'array[0].sarray[1].ssarray[7].sssarray[0]': 'array[1].sarray[3].ssarray[7].sssarray[1]', 'array[0].sarray[1].ssarray[8].sssarray[0]': 'array[1].sarray[3].ssarray[8].sssarray[1]', 'array[0].sarray[1].ssarray[9].sssarray[0]': 'array[1].sarray[3].ssarray[9].sssarray[1]', 'array[0].sarray[1].ssarray[10].sssarray[0]': 'array[1].sarray[3].ssarray[10].sssarray[1]', 'array[0].sarray[1].ssarray[11].sssarray[0]': 'array[1].sarray[3].ssarray[12].sssarray[1]', 'array[0].sarray[1].ssarray[12].sssarray[0]': 'array[1].sarray[3].ssarray[13].sssarray[1]', 'array[0].sarray[1].ssarray[13].sssarray[0]': 'array[1].sarray[3].ssarray[14].sssarray[1]', 'array[0].sarray[1].ssarray[14].sssarray[0]': 'array[1].sarray[3].ssarray[15].sssarray[1]', 'array[0].sarray[1].ssarray[15].sssarray[0]': 'array[1].sarray[3].ssarray[16].sssarray[1]' }
Tests:
reduce
var prepareMap = _.reduce( fieldsMap, function(result, value, key) { result.push({ from: key, to: value }); return result; }, [] );
map
var prepareMap = _.map(fieldsMap, function(value, key) { return { from: key, to: value } });
light
var prepareMap = [] var i = -1, props = [], length, key; for (key in fieldsMap) { props.push(key); } length = props.length; while (length--) { var key = props[++i]; prepareMap[i] = { from: key, to: fieldsMap[key] }; }
map2
var prepareMap = _.map(fieldsMap, function(value, key) { return { from: key, to: value } });
light 2
var prepareMap = [] var i = -1, props = [], length = 0; for (var key in fieldsMap) { props.push(key); ++length; } while (length--) { var key = props[++i]; prepareMap[i] = { from: key, to: fieldsMap[key] }; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
reduce
map
light
map2
light 2
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):
Measuring the performance of different approaches to create a map from an object can be a complex task, as it involves understanding various aspects such as data structure optimization, algorithmic complexity, and JavaScript engine specifics. **Benchmark Description** The benchmark consists of four test cases: 1. `reduce` 2. `map` 3. `light` (a custom implementation) 4. `map2` (another custom implementation) Each test case creates a map from the `fieldsMap` object using a different approach. **Approaches** Here's a brief explanation of each approach: 1. **`reduce`**: This method uses the `_.reduce()` function to create a new array with the specified values. The reducer function iterates over the `fieldsMap` object, creating an object with key-value pairs. 2. **`map`**: This method uses the `_.map()` function to create a new array with the specified values. Each iteration of the map function creates an object with key-value pairs from the `fieldsMap` object. 3. **`light`** and **`map2`**: These custom implementations use a simple loop to iterate over the `fieldsMap` object, creating an object with key-value pairs. **Optimizations** The custom implementations (`light` and `map2`) are likely optimized for performance by: * Avoiding unnecessary function calls * Using incrementing indices instead of `for...in` * Minimizing array allocations **JavaScript Engine Specifics** The benchmark results show that the JavaScript engine, in this case, Chrome 81 on a Mac OS X 10.14.6 system, is more efficient for the `map` and `reduce` methods compared to the custom implementations (`light` and `map2`). This might be due to: * Built-in optimizations within the `_` function or `Array.prototype.map()`/`Array.prototype.reduce()` * Lower overhead for function calls in native code **Conclusion** The results suggest that using built-in methods like `_.map()` or `Array.prototype.map()` and `_.reduce()` can provide better performance compared to custom implementations. However, the actual performance gain depends on the specific use case and requirements. In this benchmark, the `map` method performed better than the `light` implementation (`ExecutionsPerSecond: 845963.875` vs. `ExecutionsPerSecond: 521735.6875`). The `reduce` method was slower than both the optimized `map` and `custom implementations` (`ExecutionsPerSecond: 838099.625` vs. `ExecutionsPerSecond: 521735.6875`). Keep in mind that this is a simple benchmark, and real-world scenarios might have different results due to factors like memory allocation, garbage collection, or other external influences. To further improve performance, consider: * Optimizing your custom implementation for the specific use case * Using a faster data structure (e.g., `Map` instead of an object) * Avoiding unnecessary allocations and reassignments By understanding the trade-offs between different approaches and optimizations, you can write more efficient code that meets your performance requirements.
Related benchmarks:
boom2213243143134daadfadf
map vs reduce at mapping
flatMap vs reduce test
flatMap vs reduce small array
Recursive reduce vs recursive flatMap
Comments
Confirm delete:
Do you really want to delete benchmark?