Instantly share code, notes, and snippets.

Embed
What would you like to do?
String(10) VS 10 + ''

Speed test Javascript code

String(int) VS 10 + ''

Code

const list = []
const startTime = new Date();
for(let i = 0; i < 10000000; i++){
  // list.push(i + '')
  list.push(String(i))                                                      
}           
const endTime = new Date();             
console.log(endTime - startTime + 'ms')

measurements

int + '' String(int)
1 1342ms 1390ms
2 1353ms 1365ms
3 1331ms 1423ms
4   1366ms         1413ms
5   1356ms         1417ms
6     1500ms         1434ms
7     1437ms         1426ms
8     1421ms         1479ms
9     1485ms         1389ms
10    1429ms         1455ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment