var world = "World";
result = `Hello ${world}`
result = "Hello " + world
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
inter | |
concat |
Test name | Executions per second |
---|---|
inter | 3383394.2 Ops/sec |
concat | 3505015.5 Ops/sec |
Let's dive into the world of MeasureThat.net and understand what's being tested in this benchmark.
Benchmark Overview
The benchmark is comparing two approaches to concatenate strings: template literals (using the backtick notation) and the traditional +
operator.
Options Compared
There are only two options being compared:
) notation to insert values into a string. The syntax is
result = backtick notations world`.+
Operator: This approach uses the +
operator to concatenate strings. The syntax is result = "Hello " + world
.Pros and Cons
Here are some pros and cons of each approach:
+
operator due to the creation of a new string object.+
Operator (concat):Library and Special Features
There are no libraries being used in this benchmark. However, it's worth noting that template literals were introduced in ECMAScript 2015 (ES6), so if you're using an older version of JavaScript, this feature might not be available.
Other Considerations
The benchmark is running on a Chrome 90 browser on a Linux desktop platform. This means the results may not be representative of other browsers or platforms.
Alternative Approaches
If you're looking for alternative approaches to string concatenation, here are a few:
replace()
: Instead of using template literals or the +
operator, you can use the replace()
method to insert values into a string.In conclusion, this benchmark is testing two common approaches to string concatenation in JavaScript: template literals and the traditional +
operator. The results will help measure which approach is more efficient and readable.