Skip to content

Instantly share code, notes, and snippets.

@mattn
Created November 15, 2016 15:31
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 mattn/b2fc9e4353a451a74dbc05f783b4d130 to your computer and use it in GitHub Desktop.
Save mattn/b2fc9e4353a451a74dbc05f783b4d130 to your computer and use it in GitHub Desktop.
var s1 = "http://"
var s2 = "ftp://"
var s3 = "htts://"
var total = 100000000;
var n;
var start;
n = 0;
start = Date.now();
for (var i = 0; i < total; i++) {
switch (i % 3) {
case 0:
if (s1[0] == 'h' && s1.substring(0, 4) === 'http') n++;
break
case 1:
if (s2[0] == 'h' && s2.substring(0, 4) === 'http') n++;
break
case 2:
if (s3[0] == 'h' && s3.substring(0, 4) === 'http') n++;
break
}
}
console.assert(n == Number(total / 3 + 0.5).toFixed() * 1);
console.log(Date.now() - start);
n = 0;
start = Date.now();
for (var i = 0; i < total; i++) {
switch (i % 3) {
case 0:
if (s1.substring(0, 4) === 'http') n++;
break
case 1:
if (s2.substring(0, 4) === 'http') n++;
break
case 2:
if (s3.substring(0, 4) === 'http') n++;
break
}
}
console.assert(n == Number(total / 3 + 0.5).toFixed() * 1);
console.log(Date.now() - start);
n = 0;
start = Date.now();
for (var i = 0; i < total; i++) {
switch (i % 3) {
case 0:
if (s1.startsWith('http')) n++;
break
case 1:
if (s2.startsWith('http')) n++;
break
case 2:
if (s3.startsWith('http')) n++;
break
}
}
console.assert(n == Number(total / 3 + 0.5).toFixed() * 1);
console.log(Date.now() - start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment