var testString = "testing\nNewlines\nBleh"
var [value1, value2, value3] = testString.split("\n");
var regex = /.+((?=\n)|$)/g
var [value1, value2, value3] = testString.match(regex);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Splitting | |
Regex |
Test name | Executions per second |
---|---|
Splitting | 10127034.0 Ops/sec |
Regex | 6767704.5 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare the performance of two approaches: splitting a string using the split()
method, and using regular expressions (regex) to achieve the same result. The goal is to determine which approach is faster for this specific use case.
Options Compared
There are two options being compared:
split()
method to split the input string into an array of substrings, using \n
as the delimiter./.+((?=\\n)|$)/g
) to match one or more characters (including newlines) and then captures the matched substring.Pros and Cons
Split() Method:
Pros:
Cons:
\r\n
)\n
in this case)Regex Approach:
Pros:
Cons:
Other Considerations
The benchmark also considers the device platform (Desktop), operating system (Linux), and the browser version (Firefox 115) used by the test users. These factors can impact the performance results, as different browsers may handle JavaScript execution differently.
Library and Special JS Feature
There is no specific library mentioned in this benchmark. However, it's worth noting that some browsers may have built-in optimizations or features that could affect the results, such as:
String.prototype.split()
: Some browsers may use a caching mechanism to store the result of previous splits, which can improve performance.RegExp
objects: Browsers may optimize RegExp objects using techniques like caching or precompilation.Benchmark Preparation Code
The provided script preparation code is:
var testString = "testing\nNewlines\nBleh";
This code creates a sample input string with multiple newlines. The script also defines two variables, value1
, value2
, and value3
, which will store the results of the benchmark.
Alternatives
If you want to run this benchmark on your own machine, you can use the following alternatives:
Keep in mind that running a benchmark on your own machine may not produce identical results as the ones reported by MeasureThat.net, which is a cloud-based service.