Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce vs map & join
(version: 0)
Comparing performance of:
for-i vs reduce vs map & join vs for-of
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var strings = [], prefix = "abc"; for (var i = 0; i < 1000; i++) { strings.push("" + i + i); }
Tests:
for-i
let result = ""; const len = strings.length; for (let i = 0; i < len; i++) { result += prefix + strings[i]; }
reduce
const result = strings.reduce((accum, el) => accum + prefix + el, "");
map & join
const result = strings.map(el => prefix + el).join("");
for-of
let result = ""; for (const el of strings) { result += prefix + el; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for-i
reduce
map & join
for-of
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):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares four different approaches to concatenate an array of strings: 1. Using a traditional `for` loop (`for-i`) 2. Using a `for...of` loop with a string concatenation (`for-of`) 3. Using the `reduce()` method to concatenate the strings (`reduce`) 4. Using the `map()` and `join()` methods together to concatenate the strings (`map & join`) **Script Preparation Code** The script preparation code creates an array of 1000 strings, where each string is a concatenation of two numbers (i and i) prefixed with "abc". This array will be used as input for the benchmark. **Test Cases** Each test case has a unique `Benchmark Definition` JSON object that defines how to perform the concatenation. The main difference between these definitions is: * **for-i**: Uses a traditional `for` loop to iterate over the strings and concatenate them. * **reduce**: Uses the `reduce()` method, which applies a function to each element of the array (in this case, concatenating two strings). * **map & join**: Uses the `map()` method to create an array with modified versions of the strings (prefixing each string with "abc"), and then uses the `join()` method to concatenate these strings. * **for-of**: Uses a `for...of` loop to iterate over the strings and concatenate them, similar to the traditional `for-i` approach. **Library: String** Both the `reduce()` and `map()` methods use the built-in `String` class in JavaScript. The `join()` method also uses the `String` class. **JavaScript Feature/Syntax: Closures** The `reduce()` method uses a closure to accumulate the result of each iteration. This is a fundamental concept in JavaScript, where a closure is created when a function has access to its own scope and the scope of its outer functions. **Benchmark Result Interpretation** The benchmark results show the number of executions per second (in millions) for each test case on different browsers and devices. The browser with the highest execution rate for each test case will likely be the fastest in terms of performance. In summary, this benchmark compares four approaches to concatenate an array of strings: * Traditional `for` loop (`for-i`) * `for...of` loop with string concatenation (`for-of`) * `reduce()` method * `map()` and `join()` methods together Each approach has its pros and cons. For example, the traditional `for` loop is straightforward but may be less efficient than other approaches. The `map()` and `join()` methods are often more concise and expressive, but may have additional overhead due to function calls.
Related benchmarks:
Reduce vs map/join
Array<string>.join vs Array<string>.reduce
map and join vs reduce
map and join vs reduce small array
Comments
Confirm delete:
Do you really want to delete benchmark?