Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Nothing QWERTY
(version: 0)
Comparing performance of:
foreach vs for vs map
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 1000; i++) { arr[i] = { dataValues: { "code": "GB!BA" + i, "crsCode": null, "nlcCode": null, "type": "Station", "name": "Barrow+Bus", "mainStation": "NO", "countryCode": "GB", "timeZone": "Europe/London", "latitude": null, "longitude": null, "stateProvinceCode": null, "itineraryStation": "OUTPUT_ONLY", "fareStation": "OUTPUT_ONLY", "status": null, "chilternStation": null, "preferredStation": null, "createdAt": "2017-10-05T16:22:16.000Z", "updatedAt": "2018-06-25T10:28:01.000Z", "suppliers": "[{\"memberStations\": [],\"groupStations\": [],\"source\": \"ATOC\",\"type\": \"ALIAS\",\"codeType\": null,\"typeName\": null,\"alias\": \"Barrow+Bus\"}]", "smartCardSupport": false }}; } function someFn(model) { const interfce = { code: model.dataValues.code, crsCode: model.dataValues.crsCode, nlcCode: model.dataValues.nlcCode, type: model.dataValues.type, name: model.dataValues.name, mainStation: model.dataValues.mainStation, iataCode: model.dataValues.iataCode, countryCode: model.dataValues.countryCode, timeZone: model.dataValues.timeZone, latitude: model.dataValues.latitude, longitude: model.dataValues.longitude, city: model.dataValues.city, stateProvinceCode: model.dataValues.stateProvinceCode, itineraryStation: model.dataValues.itineraryStation, fareStation: model.dataValues.fareStation, status: model.dataValues.status, chilternStation: model.dataValues.chilternStation, preferredStation: model.dataValues.preferredStation, createdAt: model.dataValues.createdAt, updatedAt: model.dataValues.updatedAt, suppliers: model.dataValues.suppliers, smartCardSupport: model.dataValues.smartCardSupport }; return interfce; }
Tests:
foreach
var newArray = []; arr.forEach(function (item){ var interface = someFn(item); newArray.push(interface); })
for
var newArray = []; for (var i = 0, len = arr.length; i < len; i++) { var interface = someFn(arr[i]); newArray.push(interface); }
map
var newArray = arr.map(item => someFn(item));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
foreach
for
map
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'd be happy to help explain what's being tested in the provided JSON. **Benchmark Definition** The benchmark definition provides a script that prepares data for testing, which includes: * An array `arr` with 1000 objects, each containing various properties. * A function `someFn` that takes an object from `arr` and returns an object with all the same properties, but with some additional ones (e.g., `interface`). **Individual Test Cases** There are three test cases: 1. **foreach**: Tests how fast the code is when using a `forEach` loop to iterate over `arr`. The script creates an empty array `newArray`, then iterates over `arr` using `forEach`, calling `someFn` on each object and pushing the result into `newArray`. 2. **for**: Tests how fast the code is when using a traditional `for` loop to iterate over `arr`. Similar to `foreach`, but uses a manual loop variable. 3. **map**: Tests how fast the code is when using the `map` method to create a new array with transformed elements from `arr`. The script creates an empty array `newArray`, then calls `map` on `arr` and assigns it to `newArray`. **What's Being Tested** The benchmark tests the performance of each iteration approach: * How fast is the code when using `forEach`? * How fast is the code when using a traditional `for` loop? * How fast is the code when using the `map` method? **Options Compared** The options compared are: * `forEach` * Traditional `for` loop * `map` **Pros and Cons of Each Approach** Here's a brief summary: * **forEach**: Pros: concise, easy to read. Cons: may have performance overhead due to function call. * **Traditional for loop**: Pros: direct control over iteration, no function call overhead. Cons: more verbose, error-prone. * **map**: Pros: concise, expressive. Cons: may have performance overhead due to array mutation. **Library Used** There is no explicit library mentioned in the benchmark definition or test cases. However, `forEach` and `for` are built-in JavaScript functions, while `map` is also a built-in method on arrays. **Special JS Features or Syntax** The only special feature used here is the spread operator (`=>`) for arrow function definitions, which was introduced in ECMAScript 2015 (ES6). The `map` method is also part of ES6 syntax. No other special features are mentioned. **Other Alternatives** If you're looking for alternative iteration methods or libraries, consider: * `reduce`: Another array method that can be used instead of `forEach` and `for`. * `iterable` APIs (e.g., `Array.prototype.entries()`, `Array.prototype.keys()`): Can provide more control over iteration than built-in functions. * Third-party libraries like Lodash or Ramda: Provide additional functional programming utilities, including iterative methods. Keep in mind that the choice of iteration method depends on your specific use case and performance requirements.
Related benchmarks:
Array loop vs foreach vs map station
test lodash vs js vanilla
Test HOY
array iteration vs _.each vs map vs for..of vs for loop AP1
Comments
Confirm delete:
Do you really want to delete benchmark?