Created
January 22, 2019 19:38
-
-
Save areknawo/02e7125219e1f41885eebe6a2406a6d8 to your computer and use it in GitHub Desktop.
Untitled benchmark (http://jsbench.github.io/#02e7125219e1f41885eebe6a2406a6d8) #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>Untitled 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; | |
Benchmark.prototype.setup = function () { | |
const el = document.createElement("div"); | |
document.body.appendChild(el); | |
const style = { | |
width: "10px", | |
height: "20px", | |
background: "red" | |
} | |
el.style = style; | |
function diffAndSet(a, b){ | |
for(const key in b){ | |
if(!a[key] || (a[key] && a[key] !== b[key])){ | |
el.style[key] = b[key] | |
} | |
} | |
} | |
function stringAndSet(a, b){ | |
let aStr = ""; | |
let bStr = ""; | |
for(const key in a){ | |
aStr += `${key}:${a[key]};` | |
} | |
for(const key in b){ | |
bStr += `${key}:${b[key]};` | |
} | |
if(bStr !== aStr){ | |
el.style.cssText=bStr; | |
} | |
} | |
}; | |
suite.add("diffAndSet(style, {width: \"10px\", height: \"100px\", background: \"green\", top: \"10px\", bottom: \"50%\", color: \"orange\", left: \"10px\", right: \"20px\", borderRadius: \"100px\"})", function () { | |
diffAndSet(style, {width: "10px", height: "100px", background: "green", top: "10px", bottom: "50%", color: "orange", left: "10px", right: "20px", borderRadius: "100px"}) | |
}); | |
suite.add("stringAndSet(style, {width: \"10px\", height: \"100px\", background: \"green\", top: \"10px\", bottom: \"50%\", color: \"orange\", left: \"10px\", right: \"20px\", borderRadius: \"100px\"})", function () { | |
stringAndSet(style, {width: "10px", height: "100px", background: "green", top: "10px", bottom: "50%", color: "orange", left: "10px", right: "20px", borderRadius: "100px"}) | |
}); | |
suite.add("Object.assign(el.style, {width: \"10px\", height: \"100px\", background: \"green\", top: \"10px\", bottom: \"50%\", color: \"orange\", left: \"10px\", right: \"20px\", borderRadius: \"100px\"})", function () { | |
Object.assign(el.style, {width: "10px", height: "100px", background: "green", top: "10px", bottom: "50%", color: "orange", left: "10px", right: "20px", borderRadius: "100px"}) | |
}); | |
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("Untitled 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