Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native JS: concatenate string with + vs template literals vs String.concat with Function.
(version: 0)
find best solution for concatenate 4 strings
Comparing performance of:
using plus operator vs using concat function vs using template literals
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var name = "name"; var id = "id"; var platform = "haha"; function getPlatform(){ return platform; }
Tests:
using plus operator
for (let i = 0; i < 80; ++i) { let result = id + ": 1, " + name + ": someItem : " + getPlatform(); }
using concat function
for (let i = 0; i < 80; ++i) { let result = "".concat(id, ": 1, ", name, ": someItem : " + getPlatform()); }
using template literals
for (let i = 0; i < 80; ++i) { let result = `${id}: 1, ${name}: someItem : ${getPlatform()}`; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
using plus operator
using concat function
using template literals
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's being tested. **Overview** The benchmark compares three ways to concatenate strings in JavaScript: 1. Using the `+` operator (also known as string concatenation) 2. Using template literals 3. Using the `concat()` function with a `Function` object **Test Case: Native JS - concatenate string with + vs template literals vs String.concat with Function** The test case is designed to measure which approach performs best for concatenating four strings. **Script Preparation Code** The script preparation code defines three variables: * `name`: set to "name" * `id`: set to "id" * `platform`: set to "haha" (note: this variable is only used within the test case and not in the actual benchmark) A function `getPlatform()` returns the value of `platform`. **Html Preparation Code** There is no HTML preparation code provided. **Test Cases** The benchmark has three individual test cases: 1. **using plus operator**: This test case uses the `+` operator to concatenate the strings. 2. **using concat function**: This test case uses the `concat()` function with a `Function` object to concatenate the strings. 3. **using template literals**: This test case uses template literals (introduced in ECMAScript 2015) to concatenate the strings. **Library and Special JS Features** The only library used in this benchmark is: * `String`: The built-in JavaScript string type, which provides various methods for manipulating strings, including concatenation. There are no special JavaScript features or syntax mentioned in this benchmark. **Pros and Cons of Each Approach** 1. **using + operator**: * Pros: Simple and widely supported. * Cons: Can be slow due to the overhead of creating intermediate strings. 2. **using concat function**: * Pros: Allows for more control over the concatenation process, as you can pass arguments to the `concat()` method. * Cons: Requires a `Function` object to be created and passed to `concat()`, which can lead to performance overhead. 3. **using template literals**: * Pros: Modern and efficient way to concatenate strings, with better performance than traditional string concatenation. * Cons: Requires ECMAScript 2015 or later support. **Other Alternatives** In addition to these three approaches, other methods for concatenating strings in JavaScript include: 1. `String.prototype +=` 2. Using a library like Lodash 3. Using a utility function like `strings.concat` However, these alternatives may have different performance characteristics and are not explicitly tested in this benchmark. **Benchmark Results** The latest benchmark results show the execution times for each test case on Chrome 103 running on a Desktop with Mac OS X 10.15.7. The results indicate that using template literals is the fastest approach.
Related benchmarks:
Native JS: concatenate string with + vs template literals vs String.concat - My test
Native JS: concatenate string with + vs template literals vs String.concat_0
Native JS: concatenate string with + vs template literals vs String.concat + numeric hash
Native JS2: concatenate string with + vs template literals vs String.concat
Comments
Confirm delete:
Do you really want to delete benchmark?