Skip to content

Instantly share code, notes, and snippets.

@vidul-nikolaev-petrov
Last active November 29, 2016 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vidul-nikolaev-petrov/a6f615373bbb0fd3fd985418926606fa to your computer and use it in GitHub Desktop.
Save vidul-nikolaev-petrov/a6f615373bbb0fd3fd985418926606fa to your computer and use it in GitHub Desktop.
String performance test on Node -- index, slice, substring
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