Skip to content

Instantly share code, notes, and snippets.

@andredumas
Last active August 22, 2016 06:39
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 andredumas/7a3277e3cb06c35f4ba34562a873cf35 to your computer and use it in GitHub Desktop.
Save andredumas/7a3277e3cb06c35f4ba34562a873cf35 to your computer and use it in GitHub Desktop.
JsPerf

JsPerf

Test cases for javascript browser profiling.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>String Concat</title>
</head>
<body>
Status: <span id="status">Idle</span>
<script>
function run() {
document.getElementById("status").textContent = "Running";
array = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
function join(array) {
return array.join(' ');
}
function reduce(array) {
return array.reduce(function(p, c) {
return p + ' ' + c;
});
}
function arrayConcat(array) {
if(!array.length) return;
var result = array[0];
for(var i = 1; i < array.length; i++) {
result += ' ' + array[i];
}
return result;
}
function arrayConcatCacheLength(array) {
var length = array.length;
if(!length) return;
var result = array[0];
for(var i = 1; i < length; i++) {
result += ' ' + array[i];
}
return result;
}
function staticConcat() {
return 'A' + ' ' + 'B' + ' ' + 'C' + ' ' + 'D' + ' ' + 'E' + ' ' + 'F' + ' ' + 'G';
}
for (var i = 0; i < 100000; i++) {
join(array);
reduce(array);
arrayConcat(array);
arrayConcatCacheLength(array);
staticConcat();
}
document.getElementById("status").textContent = "DONE!";
console.log('DONE!');
}
setTimeout(run, 1000);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Date Object Check & getTime</title>
</head>
<body>
Status: <span id="status">Idle</span>
<script>
function run() {
document.getElementById("status").textContent = "Running";
function auto(value) {
return +value;
}
function instance(value) {
return value instanceof Date ? value.getTime() : value;
}
function duck(value) {
return value.getTime ? value.getTime() : value;
}
var number, date;
for (var i = 0; i < 100000; i++) {
number = Math.floor(Math.random()*1000);
date = new Date(number);
auto(number);
auto(date);
instance(number);
instance(date);
duck(number);
duck(date);
}
document.getElementById("status").textContent = "DONE!";
console.log('DONE!');
}
setTimeout(run, 1000);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Number To String</title>
</head>
<body>
Status: <span id="status">Idle</span>
<script>
function run() {
document.getElementById("status").textContent = "Running";
function toString(value) {
return value.toString();
}
function constructor(value) {
return String(value);
}
function implicit(value) {
return '' + value;
}
var number;
for (var i = 0; i < 100000; i++) {
number = Math.floor(Math.random()*1000);
toString(number);
constructor(number);
implicit(number);
}
document.getElementById("status").textContent = "DONE!";
console.log('DONE!');
}
setTimeout(run, 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment