Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string literal vs array join
(version: 9)
Comparing performance of:
array.join vs string literal vs string concatenation
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = Array(50000) .fill(1) .map(() => 'A' + Math.ceil(Math.random() * 100));
Tests:
array.join
var res=arr.join('; ');
string literal
var finalString2 = arr.reduce((res, item) => { return `${res}; ${item}`; }, '');
string concatenation
var finalString3 = arr.reduce((res, item) => { return res + '; ' + item; }, '');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
array.join
string literal
string concatenation
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.join
490.2 Ops/sec
string literal
734.2 Ops/sec
string concatenation
904.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **What is being tested?** The benchmark measures the performance of different approaches to concatenate strings in JavaScript: 1. `string literal` (using template literals or string concatenation with the `+` operator) 2. `array.join` (using the `join()` method on an array of strings) 3. `string concatenation` (using the `+` operator, similar to `string literal`, but without using template literals) **Options compared** Each test case compares two or more approaches to concatenate strings. The main options being tested are: * `array.join`: uses the `join()` method on an array of strings * `string literal` (template literals or string concatenation with the `+` operator) * `string concatenation` (using the `+` operator, similar to `string literal`, but without using template literals) **Pros and cons of each approach** Here's a brief summary: * `array.join`: + Pros: efficient, as it uses a built-in method that can handle large arrays + Cons: may be slower for small arrays or strings due to the overhead of the `join()` method * `string literal` (template literals): + Pros: readable and concise, with no need for explicit concatenation + Cons: may not be supported in older browsers or environments that don't understand template literals * `string concatenation`: + Pros: familiar and widely supported, as it's the most common way to concatenate strings in JavaScript + Cons: can lead to performance issues if done excessively (e.g., many explicit concatenations) **Library usage** There is no library explicitly mentioned in the benchmark code. However, template literals are a built-in feature of modern JavaScript engines. **Special JS features or syntax** * Template literals (used in `string literal`) were introduced in ECMAScript 2015 (ES6) and provide a concise way to concatenate strings with expressions. * The `join()` method on arrays is also a built-in feature, introduced in ECMAScript 2009 (ES5). **Other alternatives** If you're looking for alternative approaches to concatenate strings, here are some examples: * Using a loop: `for (var i = 0; i < arr.length; i++) { output += arr[i]; }` * Using a string builder library (e.g., `string-pump` or `lodash-string`) * Using a regex to concatenate strings (although this is generally not recommended) Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the approaches tested in the benchmark.
Related benchmarks:
Fill array with random integers
JSON.strigify vs array.join
Flatten Array of Arrays
.at vs [x]
at 500 vs [500]
Comments
Confirm delete:
Do you really want to delete benchmark?