var s1 = "https://www.measurethat.net/Benchmarks/Show/2131/0/array-split-vs-string-substring";
var s2 = "foo";
var n1 = s1.split("/")[0];
var n2 = s2.split("/")[0];
var n1 = s1.substring(0, s1.indexOf("/"));
var n2 = s2.substring(0, s2.indexOf("/"));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
split | |
substring |
Test name | Executions per second |
---|---|
split | 2598950.8 Ops/sec |
substring | 3453513.2 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The provided benchmark tests two approaches to split or extract a certain part from a string: split
and substring
. The input strings are defined in the "Script Preparation Code" section:
s1 = "https://www.measurethat.net/Benchmarks/Show/2131/0/array-split-vs-string-substring"
(a URL)
s2 = "foo"
(a single-character string)
The benchmark tries to extract the first part of s1
and s2
, which is a single character /
followed by the rest of the string.
Options compared
There are two approaches being tested:
split
method: This method splits the input string into an array of substrings using a specified separator (in this case, /
). The first element of the resulting array will contain the desired part.substring
method: This method extracts a specified number of characters from the start of the input string.Pros and Cons of each approach
split
method:substring
for small strings or when only a single character is extracted.substring
method:indexOf
).Library and syntax
There is no specific library used in this benchmark. The split
method is a built-in JavaScript method, while the substring
method is also built-in but requires an explicit call to indexOf
to extract the desired part.
Special JS feature or syntax
No special JavaScript features or syntax are being tested in this benchmark.
Other alternatives
If the split
and substring
methods were not available, other approaches could be used:
indexOf
, replace
) might be used.In summary, this benchmark compares two common approaches for extracting parts from strings in JavaScript: split
and substring
. The split
method is more flexible but may incur additional overhead, while the substring
method is typically faster for small strings but has limited flexibility.