<html>
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js'></script>
</head>
<body></body>
</html>
window.testData = {}
for (i = 0; i < 10000; i++) {
window.testData['key' + i] = i
}
R.forEachObjIndexed((value, key) => console.log(value), window.testData)
Object.values(window.testData).map(value => console.log(value))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Ramda (while loop inside) | |
Native ES5 Object.values + Array.map |
Test name | Executions per second |
---|---|
Ramda (while loop inside) | 15.8 Ops/sec |
Native ES5 Object.values + Array.map | 15.2 Ops/sec |
I'd be happy to explain the benchmark and its results.
Benchmark Overview
The benchmark compares the performance of two approaches:
forEachObjIndexed
: This method is part of the Ramda library, which provides a functional programming style for JavaScript. It iterates over an object's keys and values simultaneously.Object.values + Array.map
: This approach uses native JavaScript methods to extract values from an object and log them in an array.Benchmark Definition JSON
The benchmark definition JSON contains the following information:
window.testData
object with 10,000 key-value pairs, where each value is the corresponding key incremented by 1.Individual Test Cases
The benchmark consists of two individual test cases:
forEachObjIndexed
method from Ramda, which iterates over an object's keys and values simultaneously using a while loop.window.testData
object and log them in an array using Object.values
and Array.map
.Library Used
In this benchmark, Ramda is used as a library for providing the functional programming style and forEachObjIndexed
method.
Special JS Feature/Syntax
There are no special JavaScript features or syntax mentioned in this benchmark. The code uses standard ES5 syntax and relies on native JavaScript methods.
Benchmark Results
The latest benchmark result contains two test cases, each with a different approach:
forEachObjIndexed
method with a while loop.The results indicate that the native ES5 approach outperforms the Ramda approach.
Other Alternatives
Some other alternatives for this benchmark could be:
Array.prototype.forEach
instead of forEachObjIndexed
However, it's worth noting that the choice of approach depends on the specific use case and requirements. For example, if you need to perform operations on objects with complex data structures, Ramda's functional programming style might be more suitable.
In summary, this benchmark compares the performance of two approaches for iterating over an object's values: Ramda's forEachObjIndexed
method and native ES5 methods (Object.values + Array.map
). The benchmark results show that the native ES5 approach outperforms the Ramda approach.