Skip to content

Instantly share code, notes, and snippets.

@Chunlin-Li
Created November 26, 2015 04:33
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 Chunlin-Li/788d0f64fdf46ccca40c to your computer and use it in GitHub Desktop.
Save Chunlin-Li/788d0f64fdf46ccca40c to your computer and use it in GitHub Desktop.
node / javascript performance:

Performance Comparison:

node version : v4.1.2 platform: Intel I7, 16G DDR3, Ubuntu x64

Section 1

use '+' to concatenate string and variables

Section 2

use template literals to concatenate string and variables.

start = process.hrtime();
for(let i = 0 ; i < N; i++) {
    string = 'asdfjkl' + i +  '#' + i;
}
console.log('section 1 : ' + timeUse(start));


start = process.hrtime();
for(let i = 0 ; i < N; i++) {
    string2 = `asdfjkl${i}#${i}`;
}
console.log('section 2 : ' + timeUse(start));

result:

# N : 10
section 1 : 17025ns
section 2 : 15570ns

# N : 100
section 1 : 27370ns
section 2 : 27577ns

# N : 1K
section 1 : 100643ns
section 2 : 138099ns

# N : 10K 
section 1 : 2815412ns
section 2 : 2257058ns

# N : 100K
section 1 : 16648973ns
section 2 : 30639108ns

conclusion:

ES6 template literal is convenient but not has performance advantage.
for performance condition, use old style to gain a litter better effiency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment