var strTxt = Array(100).fill("@".repeat(128)).join(" "), len = 128;
var ar = strTxt.match(new RegExp('.{1,'+len+'}','g'));
var ar = Array(Math.ceil(strTxt.length/len)), arlen=ar.length;
for(i=0,y=0;i<arlen;i++,y=i*len){
ar[i] = strTxt.slice(y,y+len);
}
var ar = Array(Math.ceil(strTxt.length/len)), arlen=ar.length;
for(i=0,y=0;i<arlen;i++,y=i*len){
ar[i] = strTxt.substr(y,len);
}
var ar = Array(Math.ceil(strTxt.length/len)), arlen=ar.length,i = 0;
while(i<arlen){
ar[i] = strTxt.slice(i*len,i*len+len);
i++;
}
var ar = Array(Math.ceil(strTxt.length/len)), arlen=ar.length,i = 0;
while(i<arlen){
ar[i] = strTxt.substr(i*len,len);
i++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
testttttt | |
tesstttt2 | |
tesstttt3 | |
tesstttt4 | |
tesstttt5 |
Test name | Executions per second |
---|---|
testttttt | 29930.5 Ops/sec |
tesstttt2 | 10092.1 Ops/sec |
tesstttt3 | 12460.9 Ops/sec |
tesstttt4 | 21736.1 Ops/sec |
tesstttt5 | 28377.2 Ops/sec |
Let's break down the provided benchmark and its options.
Benchmark Overview
The benchmark measures the performance of JavaScript code that splits a large string into smaller substrings. The input string, strTxt
, is generated using the Array.fill()
method with a repeating pattern of 128 characters. The length of each substring (len
) is also set to 128.
Benchmark Options
There are four different approaches compared in this benchmark:
RegExp.match()
: This approach uses a regular expression to split the string into substrings. It's implemented as var ar = strTxt.match(new RegExp('.{1,'+len+'}','g'));
.Array
and for
loop: This approach splits the string into substrings manually using an array and a for
loop. It's implemented in two ways:strTxt.slice()
: var ar = Array(Math.ceil(strTxt.length/len)), arlen=ar.length;\r\nfor(i=0,y=0;i<arlen;i++,y=i*len){\r\n\tar[i] = strTxt.slice(y,y+len);\r\n}\)
strTxt.substr()
: var ar = Array(Math.ceil(strTxt.length/len)), arlen=ar.length;\r\nfor(i=0,y=0;i<arlen;i++,y=i*len){\r\n\tar[i] = strTxt.substr(y,len);\r\n}\)
while
loop: This approach also splits the string into substrings manually, but uses a while
loop instead of a for
loop: `var ar = Array(Math.ceil(strTxt.length/len)), arlen=ar.length,i = 0;\r\nwhile(i<arlen){\r\n\tar[i] = strTxt.slice(i*len,i*len+len);\r\n\ti++;\t\r\n}``substr
: Similar to the previous approach, but uses only strTxt.substr()
: `var ar = Array(Math.ceil(strTxt.length/len)), arlen=ar.length,i = 0;\r\nwhile(i<arlen){\r\n\tar[i] = strTxt.substr(i*len,len);\r\n\ti++;\t\r\n}``Pros and Cons of Each Approach
RegExp.match()
:Array
and for
loop:Array
and for
loops.while
loop:substr
:strTxt.substr()
.Libraries Used
None of the provided benchmark code uses any external libraries. However, it's worth noting that in real-world scenarios, you might use libraries like Lodash or Underscore.js for string manipulation and iteration.
Special JavaScript Features or Syntax
There are no special JavaScript features or syntax used in this benchmark. The code follows standard JavaScript conventions.
Other Alternatives
Some alternative approaches to splitting a large string into substrings include:
String.prototype.split()
method, which is also part of the standard JavaScript API.Array.prototype.slice.call()
method, which allows for more flexibility in slicing strings.slice-string
or substring-queue
, which provide optimized implementations of substring splitting.In conclusion, this benchmark provides a straightforward comparison of four different approaches to splitting a large string into substrings. While there are pros and cons associated with each approach, the choice ultimately depends on personal preference, performance requirements, and readability considerations.