<script>
var _arr = (() => {
const produce = (n) => new Array(n + 1).fill().map(() => `${Math.random()}`.slice(2)).join(']');
const a0 = new Array(20).fill().map(() => produce(0));
const a1 = new Array(20).fill().map(() => produce(1));
const a2 = new Array(20).fill().map(() => produce(2));
const a3 = new Array(20).fill().map(() => produce(3));
const a4 = new Array(20).fill().map(() => produce(4));
const arr = [].concat(a0).concat(a1).concat(a2).concat(a3).concat(a4);
arr.sort(() => Math.random() - 0.5);
return arr;
})();
function getPositions(path) {
const positions = [];
for (let i = 0; i < path.length; i++) {
if (path[i] === ']') {
positions.push(i);
}
}
return positions;
}
function getPositionsIndexOf(path) {
const positions = [];
for (let i = 0; (i = path.indexOf(']', i)) >= 0; i++) {
positions.push(i);
}
return positions;
}
</script>
const arr = _arr;
const len = arr.length;
let r = 0;
for (let i = 0; i < len; i++) {
const reg = /\]/g;
const positions = [];
let match;
const basic_path = arr[i];
while ((match = reg.exec(basic_path)) !== null) {
positions.push(match.index);
}
r += positions.length;
}
window.tmp_r1 = r;
const arr = _arr;
const len = arr.length;
let r = 0;
for (let i = 0; i < len; i++) {
const basic_path = arr[i];
const positions = getPositions(basic_path);
r += positions.length;
}
window.tmp_r2 = r;
const arr = _arr;
const len = arr.length;
let r = 0;
for (let i = 0; i < len; i++) {
const basic_path = arr[i];
const positions = getPositionsIndexOf(basic_path);
r += positions.length;
}
window.tmp_r3 = r;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
reg.exec | |
for-loop | |
for-indexOf |
Test name | Executions per second |
---|---|
reg.exec | 140010.5 Ops/sec |
for-loop | 50403.7 Ops/sec |
for-indexOf | 105460.1 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, testing different approaches for finding character positions in an array of strings.
Benchmark Definition JSON
The provided benchmark definition JSON represents three test cases:
reg.exec
: Tests the usage of the exec()
method with a regular expression to find character positions in an array of strings.for-loop
: Tests a traditional for
loop approach to find character positions in an array of strings.for-indexOf
: Tests the usage of the indexOf()
method to find character positions in an array of strings.Options Compared
The three test cases compare different approaches for finding character positions:
]
) in each string.for
loop, calling getPositions()
or getPositionsIndexOf()
function on each element.indexOf()
method to find the index of the first occurrence of ]
in each string.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
reg.exec
for large datasets.reg.exec
for small datasets.Library Usage
The benchmark definition JSON uses two custom functions:
getPositions(path)
: Returns an array of indices where ]
appears in the input string path
.getPositionsIndexOf(basic_path)
: Similar to getPositions()
, but uses the indexOf()
method instead.These functions are likely implemented by the user, as they don't appear to be part of a standard JavaScript library.
Special JS Features or Syntax
There are no special JS features or syntax mentioned in this benchmark definition. The focus is on comparing different approaches for finding character positions.
Alternatives
Other alternatives for finding character positions could include:
lodash
with its indexOf()
and findIndex()
methods.