Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forIn vs forOf vs optimised forOf
(version: 0)
Comparing performance of:
for in vs for of vs for of optimised
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
//set up var abc = new Object() for(var intCtr=0; intCtr<1000; intCtr++) { abc[intCtr+''] = Math.floor(Math.random() * Math.floor(10000)) }
Tests:
for in
var forInResult = 0; for(var key in abc) { forInResult += abc[key] }
for of
var forOfResult = 0; for(var key of Object.keys(abc)) { forOfResult += abc[key] }
for of optimised
var forOfKeys = Object.keys(abc); var forOfKeysResult = 0; for(var key of forOfKeys) { forOfKeysResult += abc[key]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for in
for of
for of optimised
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):
I'll break down the benchmark for you. **Benchmark Overview** The benchmark compares three different approaches to iterate over an object: 1. `for-in` loop 2. `for...of` loop (with `Object.keys()` method) 3. Optimized `for...of` loop (using `Object.keys()` method) **What is tested?** In this benchmark, the script prepares a large object with 1000 properties, each with a random value between 0 and 10,000. The test cases then use these objects to calculate the sum of their values using three different iteration methods: * `for-in` loop: This method uses the `in` operator to iterate over the object's own enumerable properties. * `for...of` loop (with `Object.keys()`): This method uses the `Object.keys()` method to get an array of the object's property names, and then iterates over that array using a `for...of` loop. * Optimized `for...of` loop: This method is similar to the previous one, but it uses the `Object.keys()` method only once to get the array of property names, and then reuses that array in the loop. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * `for-in` loop: + Pros: Simple and easy to understand. + Cons: Can be slower due to the overhead of checking if properties exist using `in`. + Note: This method is deprecated since ECMAScript 5.1, but it's still supported for backward compatibility. * `for...of` loop (with `Object.keys()`): + Pros: Fast and efficient, as it avoids the `in` operator lookup. + Cons: Requires creating an array of property names using `Object.keys()`, which can be slower than a direct iteration. * Optimized `for...of` loop: + Pros: Combines the benefits of both approaches by reusing the array of property names, reducing overhead. + Cons: Still requires creating an array of property names, but it's optimized to avoid repeated lookups. **Library and Special JS Feature** The benchmark uses the `Object.keys()` method, which is a part of the ECMAScript standard. This method returns an array of strings representing the property names of the given object. **Other Considerations** When iterating over objects in JavaScript, it's essential to consider the following factors: * **Iteration order**: In older browsers and older versions of Node.js, the `for-in` loop may iterate over inherited properties, which can lead to unexpected behavior. The `for...of` loop and optimized version avoid this issue by using arrays or other data structures. * **Object size**: As the object size increases, the iteration method's performance can degrade due to memory management overhead. The optimized `for...of` loop may be more efficient in such cases. **Alternatives** If you're looking for alternative benchmarking tools or libraries, consider: * Benchmark.js: A popular and widely-used benchmarking library. * WebPageTest: A comprehensive tool for testing web page performance. * Google's Chrome DevTools: Offers a built-in profiler and benchmarking features. Keep in mind that each tool has its strengths and weaknesses, and the choice ultimately depends on your specific use case and requirements.
Related benchmarks:
Lodash sortBy vs Object.values and array.prototype.sort
Unique Array: Lodash vs spread new Set vs reduce vs for - random data
Lodash ceil vs JS native Math.ceil (2022)
lodash to array vs Object to array trial
orderBy vs array.prototype.sort vs vanila orderBy vs QuickSort
Comments
Confirm delete:
Do you really want to delete benchmark?