Skip to content

Instantly share code, notes, and snippets.

@derwiki
Created June 13, 2013 17:55
Show Gist options
  • Save derwiki/5775833 to your computer and use it in GitHub Desktop.
Save derwiki/5775833 to your computer and use it in GitHub Desktop.
var ITERATIONS = 100000,
av = navigator.appVersion;
console.time('indexOf')
for (var i = 0; i < ITERATIONS; i++) {
navigator.appVersion.indexOf("Mobile") > -1
}
console.timeEnd('indexOf')
console.time('regex')
for (var i = 0; i < ITERATIONS; i++) {
/mobile/i.test(navigator.appVersion)
}
console.timeEnd('regex')
console.time('indexOf-cached');
for (var i = 0; i < ITERATIONS; i++) {
av.indexOf("Mobile") > -1
}
console.timeEnd('indexOf-cached')
console.time('regex-cached')
for (var i = 0; i < ITERATIONS; i++) {
/mobile/i.test(av)
}
console.timeEnd('regex-cached')
ndexOf: 470.218ms
regex: 433.497ms
indexOf-cached: 128.361ms
regex-cached: 123.078ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment