Last active
November 29, 2016 17:44
-
-
Save vidul-nikolaev-petrov/a6f615373bbb0fd3fd985418926606fa to your computer and use it in GitHub Desktop.
String performance test on Node -- index, slice, substring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var string = 'string', | |
stringIndex = function () { | |
string[0]; | |
}, | |
stringSlice = function () { | |
string.slice(0, 1); | |
}, | |
stringSubstring = function () { | |
string.substring(0, 1); | |
}; | |
perfTest(stringIndex, 'String Index'); | |
perfTest(stringSlice, 'String Slice'); | |
perfTest(stringSubstring, 'String Substring'); | |
function perfTest(fn, label) { | |
var timer_start = process.hrtime(), | |
timer_stop = process.hrtime; | |
console.time(label); | |
for (var i = 1e8; i--;) fn(); | |
console.timeEnd(label, timer_start - timer_stop()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment