Last active
February 26, 2020 08:36
-
-
Save wooandoo/75936e4013331c253ee19dbd67b8578a to your computer and use it in GitHub Desktop.
Promise vs promise-like #jsbench #jsperf (http://jsbench.github.io/#75936e4013331c253ee19dbd67b8578a) #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> Promise vs promise-like #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> |
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 () { | |
let count = 0 | |
const inc_count = (value) => count += value | |
const test_promise_like = () => { | |
return { | |
then: (callback) => callback(1) | |
} | |
} | |
const test_static_promise_like = () => { | |
return { | |
then: and_then(1) | |
} | |
} | |
const and_then = (value) => (callback) => callback(value) | |
const and_then_one = and_then(1) | |
const test_full_static_promise_like = () => { | |
return { | |
then: and_then_one | |
} | |
} | |
const test_promise_resolve = () => { | |
return Promise.resolve(1) | |
} | |
const test_promise = () => { | |
return new Promise((resolve, reject) => { | |
resolve(1) | |
}) | |
} | |
}; | |
suite.add("test_promise_like().then(inc_count)", function () { | |
test_promise_like().then(inc_count) | |
}); | |
suite.add("test_static_promise_like().then(inc_count)", function () { | |
test_static_promise_like().then(inc_count) | |
}); | |
suite.add("test_full_static_promise_like().then(inc_count)", function () { | |
test_full_static_promise_like().then(inc_count) | |
}); | |
suite.add("test_promise_resolve().then(inc_count)", function () { | |
test_promise_resolve().then(inc_count) | |
}); | |
suite.add("test_promise().then(inc_count)", function () { | |
test_promise().then(inc_count) | |
}); | |
suite.add("inc_count(1)", function () { | |
inc_count(1) | |
}); | |
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(" Promise vs promise-like #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