Skip to content

Instantly share code, notes, and snippets.

@developit
Last active July 25, 2023 12:44
Show Gist options
  • Save developit/ec47bed3cf0e424b78bdbc4d71cdd3a3 to your computer and use it in GitHub Desktop.
Save developit/ec47bed3cf0e424b78bdbc4d71cdd3a3 to your computer and use it in GitHub Desktop.
bare-bones exhaust() vs ternary #jsbench #jsperf #jsbench #jsperf (https://jsbench.github.io/#ec47bed3cf0e424b78bdbc4d71cdd3a3) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>bare-bones exhaust() vs ternary #jsbench #jsperf #jsbench #jsperf</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 () {
const configStrings_enabled = {
boost: {
support: 'show boosts'
}
};
const configStrings_disabled = {
boost: {
support: 'no boosts'
}
};
const configBooleans_enabled = {
boost: {
support: true
}
};
const configBooleans_disabled = {
boost: {
support: false
}
};
const x = { info: { foo: 'bar' } };
function exhaust(key) {
return mapping => mapping[key]
}
function usingExhaust(config) {
return [
exhaust(config.boost.support)({
'no boosts': undefined,
'show boosts': h(InfoHeader, { x })
}),
exhaust(config.boost.support)({
'show boosts': undefined,
'no boosts': h(TextButton, null, x.name)
})
];
}
function usingTernary(config) {
return [
config.boost.support === 'show boosts'
? h(InfoHeader, { x })
: undefined,
config.boost.support === 'show boosts'
? undefined
: h(TextButton, null, x.name)
];
}
function usingTernaryBoolean(config) {
return [
config.boost.support
? h(InfoHeader, { x })
: undefined,
!config.boost.support && h(TextButton, null, x.name)
];
}
/** React.createElement */
function h() {}
/** Some fake components to reference */
function TextButton() {}
function InfoHeader() {}
};
suite.add("exhaust() version with strings in config", function () {
// exhaust() version with strings in config
usingExhaust(configStrings_enabled);
usingExhaust(configStrings_enabled);
usingExhaust(configStrings_disabled);
usingExhaust(configStrings_disabled);
});
suite.add("ternary version with strings in config", function () {
// ternary version with strings in config
usingTernary(configStrings_enabled);
usingTernary(configStrings_enabled);
usingTernary(configStrings_disabled);
usingTernary(configStrings_disabled);
});
suite.add("ternary version with booleans in config", function () {
// ternary version with booleans in config
usingTernaryBoolean(configBooleans_enabled);
usingTernaryBoolean(configBooleans_enabled);
usingTernaryBoolean(configBooleans_disabled);
usingTernaryBoolean(configBooleans_disabled);
});
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("bare-bones exhaust() vs ternary #jsbench #jsperf #jsbench #jsperf");
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