Skip to content

Instantly share code, notes, and snippets.

@macklinu
Last active September 7, 2018 04:30
Show Gist options
  • Save macklinu/c9b7f0492a198bc5caa067c6a3fd6ccd to your computer and use it in GitHub Desktop.
Save macklinu/c9b7f0492a198bc5caa067c6a3fd6ccd to your computer and use it in GitHub Desktop.

Benchmarking the code change in gatsbyjs/gatsby#7909

Example results on my machine:

//  Model Name:	MacBook Pro
//  Model Identifier:	MacBookPro14,3
//  Processor Name:	Intel Core i7
//  Processor Speed:	2.8 GHz
//  Number of Processors:	1
//  Total Number of Cores:	4
//  Memory:	16 GB

Starting benchmark

Array#includes => true (first item) x 90,527,340 ops/sec ±0.64% (84 runs sampled)
Array#includes => true (last item) x 48,148,045 ops/sec ±0.65% (88 runs sampled)
Array#includes => false x 44,982,781 ops/sec ±1.19% (88 runs sampled)
object property access => true (first item) x 555,733,626 ops/sec ±0.62% (83 runs sampled)
object property access => true (last item) x 565,248,917 ops/sec ±0.53% (90 runs sampled)
object property access => false x 553,660,965 ops/sec ±0.71% (89 runs sampled)
Set#has => true (first item) x 88,575,457 ops/sec ±1.15% (81 runs sampled)
Set#has => true (last item) x 93,230,253 ops/sec ±0.68% (89 runs sampled)
Set#has => false x 64,147,568 ops/sec ±0.53% (87 runs sampled)
Map#get => true (first item) x 579,591,059 ops/sec ±0.63% (87 runs sampled)
Map#get => true (last item) x 573,527,387 ops/sec ±0.64% (86 runs sampled)
Map#get => false x 581,403,037 ops/sec ±0.49% (91 runs sampled)

Fastest:
- Map#get => false
- Map#get => true (first item)
#!/usr/bin/env node
const Benchmark = require("benchmark");
const suite = new Benchmark.Suite();
const supportedExtensionsArray = [`jpeg`, `jpg`, `png`, `webp`, `tif`, `tiff`];
const supportedExtensionsObject = {
jpeg: true,
jpg: true,
png: true,
webp: true,
tif: true,
tiff: true
};
const supportedExtensionsMap = new Map(
supportedExtensionsArray.map(ext => [ext, true])
);
const supportedExtensionsSet = new Set(supportedExtensionsArray);
suite
.add("Array#includes => true (first item)", function() {
supportedExtensionsArray.includes("jpeg");
})
.add("Array#includes => true (last item)", function() {
supportedExtensionsArray.includes("tiff");
})
.add("Array#includes => false", function() {
supportedExtensionsArray.includes("");
})
.add("object property access => true (first item)", function() {
supportedExtensionsObject["jpeg"];
})
.add("object property access => true (last item)", function() {
supportedExtensionsObject["tiff"];
})
.add("object property access => false", function() {
supportedExtensionsObject[""];
})
.add("Set#has => true (first item)", function() {
supportedExtensionsSet.has("jpeg");
})
.add("Set#has => true (last item)", function() {
supportedExtensionsSet.has("tiff");
})
.add("Set#has => false", function() {
supportedExtensionsSet.has("");
})
.add("Map#get => true (first item)", function() {
supportedExtensionsMap.get("jpeg");
})
.add("Map#get => true (last item)", function() {
supportedExtensionsMap.get("tiff");
})
.add("Map#get => false", function() {
supportedExtensionsMap.get("");
})
.on("start", function() {
console.log("Starting benchmark");
console.log();
})
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
const fastest = this.filter("fastest")
.map("name")
.map(name => `- ${name}`)
.join("\n");
console.log();
console.log("Fastest:");
console.log(fastest);
})
.run({ async: true });
{
"name": "gatsby-pr-7909-benchmark",
"version": "0.0.0",
"bin": "./index.js",
"dependencies": {
"benchmark": "^2.1.4",
"node": "^10.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment