JSFiddle - React, Tailwind, and code Playground
JavaScript
var totalStart = Date.now(),
iters = 10000000,
counter = 1,
string = "abc0123$%^&*abc0123$%^&*abc0123$%^&*abc0123$%^&*abc0123$%^&*abc0123$%^&*abc0123$%^&*abc0123$%^&*abc0123$%^&*abc0123$%^&*abc0123$%^&*abc0123$%^&*",
etalon = withRegex( string );
function withRegex( string ) {
return string.replace( /[^a-z0-9]/ig, '' );
};
function withRegex2( string ) {
return string.replace( /[^a-z0-9]+/ig, '' );
};
function withPureJS( string ) {
var result = '',
length = string.length;
for ( var pos = 0; pos < string.length; pos++ ) {
result += parseChar(string.charCodeAt(pos))
};
return result;
};
function parseChar(c) {
if(( c > 47 && c < 58 ) || ( c > 64 && c < 91 ) || ( c > 96 && c < 123 )) {
return String.fromCharCode(c);
}
return '';
}
function test( func, string, iters, etalon ) {
var tStart = Date.now();
var res;
for ( var i=0; i<iters; i++ ) {
res = func( string + i );
};
var tEnd = Date.now();
console.log( "test " + counter++, tEnd - tStart );
};
console.log( "Test started" );
test( withRegex, string, iters, etalon );
test( withRegex2, string, iters, etalon );
test( withPureJS, string, iters, etalon );
console.log( "Total:", Date.now()-totalStart )