Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs Ramda.mapObjIndexed
(version: 1)
Comparing performance of:
for-in vs Object.keys vs R.mapObjIndexed
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script>
Script Preparation code:
var obj = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1 };
Tests:
for-in
for (var i=10000; i > 0; i--) { for (var key in obj) { console.log(key); } }
Object.keys
for (var i=10000; i > 0; i--) { Object.keys(obj).forEach(key => console.log(key)); }
R.mapObjIndexed
for (var i=10000; i > 0; i--) { R.mapObjIndexed(function(value, key) { console.log(key) }, obj); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys
R.mapObjIndexed
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
5.6 Ops/sec
Object.keys
4.2 Ops/sec
R.mapObjIndexed
3.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and some pros and cons of each approach. **Benchmark Overview** The benchmark is designed to compare three different methods for iterating over an object: `for-in`, `Object.keys`, and `R.mapObjIndexed`. The test case uses a predefined JavaScript object with 10 properties (`obj`). **Test Cases** Each test case consists of a single "Benchmark Definition" string that represents the code snippet being tested. There are three test cases: 1. **for-in**: This method uses a traditional `for` loop to iterate over the object's properties. 2. **Object.keys**: This method uses the `Object.keys()` function to get an array of the object's property names, which is then iterated over using `forEach`. 3. **R.mapObjIndexed**: This method uses the Ramda library's `mapObjIndexed` function, which applies a transformation function to each key-value pair in the object. **Libraries and Features** * **Ramda**: A functional programming library that provides a set of higher-order functions for data manipulation and transformation. + Purpose: To provide a concise and expressive way to manipulate data. + Usage: The `R.mapObjIndexed` function is used to apply a transformation function to each key-value pair in the object. **Special JS Features/Syntax** None mentioned in this benchmark. **Pros and Cons of Each Approach** 1. **for-in** * Pros: + Simple and easy to understand. + Works well for small objects or simple iterations. * Cons: + Can be slow and inefficient due to the overhead of the `for` loop. + May not work as expected if the object has sparse properties (i.e., some properties are missing). 2. **Object.keys** * Pros: + Fast and efficient, since it uses a built-in function that returns an array of property names. + Works well for large objects or complex iterations. * Cons: + May require additional memory allocation to store the property names array. 3. **R.mapObjIndexed** * Pros: + Concise and expressive syntax, making it easy to read and write. + Can be fast and efficient, since Ramda's implementation is optimized for performance. * Cons: + Requires the Ramda library, which may add unnecessary overhead or dependencies. + May require additional memory allocation due to the object and transformation functions. **Other Alternatives** Some other methods for iterating over objects in JavaScript include: 1. **forEach**: A built-in method that iterates over an array of property names and calls a callback function with each key-value pair. 2. **for...in**: An older, deprecated method that iterates over the object's properties using a traditional `for` loop. However, these methods have their own trade-offs and limitations, such as performance overhead or compatibility issues with older browsers. In conclusion, the benchmark provides a concise comparison of three different methods for iterating over an object in JavaScript. By understanding the pros and cons of each approach, developers can choose the best method for their specific use case and performance requirements.
Related benchmarks:
Ramda values vs Object.values
Ramda values vs Object.values 0.27
ramda toPairs vs. Object.entries
ramda union vs Set for merging array with Object.keys
Comments
Confirm delete:
Do you really want to delete benchmark?