Skip to content

Instantly share code, notes, and snippets.

@autioch
Created April 19, 2016 13:45
Show Gist options
  • Save autioch/245b0eae63b638b05cad55747d271879 to your computer and use it in GitHub Desktop.
Save autioch/245b0eae63b638b05cad55747d271879 to your computer and use it in GitHub Desktop.
Node.js test
function countRegex(value) {
var match = ('' + value).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
if (!match) {
return 0;
}
return Math.max(0, (match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0));
}
function countParse(value) {
var data = parseFloat(value).toString().split('.');
if (data[1]) {
return data[1].length;
}
return 0;
}
function countNumber(value) {
var data = Number(value).toString().split('.');
if (data[1]) {
return data[1].length;
}
return 0;
}
const numbers = new Array(100000000).map(() => Math.random() * 1000);
[countRegex, countParse, countNumber].forEach((fn) => {
const start = process.hrtime();
const unusedTmp = numbers.map(fn); /* jshint ignore:line */
const stop = process.hrtime(start);
console.log(`${fn.name} ${stop[0]}s ${stop[1]}ns`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment