Created
July 30, 2018 09:00
-
-
Save amogower/5d58c427e9a8e5aa94907ad7321ece67 to your computer and use it in GitHub Desktop.
Check Test Benchmark (https://jsbench.github.io/#5d58c427e9a8e5aa94907ad7321ece67) #jsbench #jsperf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Check Test Benchmark</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
suite.add("const fail = [1, 2, 3, 8];", function () { | |
const fail = [1, 2, 3, 8]; | |
const success = [1, 2, 4, 4]; | |
const target = 8; | |
function check(array, target) { | |
let count = 0; | |
for ( let i = 0; i < array.length; ++i ) | |
{ | |
for ( let j = i + 1; j < array.length; ++j ) | |
{ | |
console.log(count++); | |
total = array[i] + array[j]; | |
if (total === target) | |
{ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
check(fail, target) | |
}); | |
suite.add("const fail = [1, 2, 3, 8];", function () { | |
const fail = [1, 2, 3, 8]; | |
const success = [1, 2, 4, 4]; | |
const target = 8; | |
function fastCheck(array, target) { | |
let count = 0; | |
let low = 0; | |
let high = array.length - 1; | |
while (low < high) { | |
console.log(count++); | |
let total = array[low] + array[high]; | |
if (total === target) { | |
return true; | |
} else if (total < target) { | |
low++; | |
} else if (total > target) { | |
high++; | |
} | |
} | |
return false; | |
} | |
fastCheck(success, target); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("Check Test Benchmark"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment