Skip to content

Instantly share code, notes, and snippets.

@NovaLynxie
Created October 19, 2021 10:18
Show Gist options
  • Save NovaLynxie/76c90bc0c4d970eeb8ce91a27bb3ccf3 to your computer and use it in GitHub Desktop.
Save NovaLynxie/76c90bc0c4d970eeb8ce91a27bb3ccf3 to your computer and use it in GitHub Desktop.
Getting data from part of an array. (http://jsbench.github.io/#76c90bc0c4d970eeb8ce91a27bb3ccf3) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Getting data from part of an array.</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>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
var array = [];
for (let item = 0; item < 100; item++) {
console.log('item:', item)
array.push(item)
console.log(array)
}
};
suite.add("Array slice method", function () {
// Array slice method
let output;
output = array.slice(0,25)
});
suite.add("Array for loop iteration", function () {
// Array for loop iteration
let output = []
let limit = 25, pos = 0;
for (let step = 0; step < limit; step++) {
output.push(array[pos+step]);
}
});
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("Getting data from part of an array.");
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