If possible, convert string to integer (v2)

Revision 2 of this benchmark created on


Description

This is useful when implementing Proxy() handlers for array indexing.

Setup

const data = [];
for(let i=0; i<1000; i++) {
	data.push(String(i) + (Math.random()>0.5 ? "x" : ""));
}

const REGEXP = /^(?:0|[1-9][0-9]*)$/;

Test runner

Ready to run.

Testing in
TestOps/sec
String(0|x)
let total = 0;
for(const d of data) {
	const n = 0|d;
	if (String(n) === s) total += n
}
ready
RegEx
let total = 0;
for(const d of data) {
	if (REGEXP.match(d)) total += +d;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.