SPromiseMeSpeedJS
The JavaScript library that promises you the fastest ES6 promises
Install / Use
/learn @anonyco/SPromiseMeSpeedJSREADME
<!--[]( https://github.com/anonyco/SPromiseMeSpeedJS/issues )-->
SPromiseMeSpeed
SPromiseMeSpeed is currently the fastest ES6 Promise javascript library and polyfill, promising a speedy implementation that is ~1-59x faster than Chrome's native promises and ~2-548x faster than Bluebird's promises (five hundread and fourty eight is not a typo). The purpose of SPromiseMeSpeed is to provide a speedy alternative to ES6 promises until browsers implement faster native promises. If all you need is a way to defer the stack level so you don't get the dreaded "Maximum Stack Call Exceeded" error, then consider using my other library, DeferStackJS, for slightly better performance.
Quick Start
To use, simply drop the following snippet of HTML code into your <head> before all of the scripts that use SPromiseMeSpeed.
<script src="https://dl.dropboxusercontent.com/s/llt6sv7y2yurn2v/SPromiseMeSpeedDEBUG.min.js?dl=0"></script>
Or, alternatively, if you want faster page loading, add a defer to every script to let the browser know that you don't call evil document.write inside your script.<br /><br />
Before:
<!doctype HTML>
<html><head>
<script src="https://dl.dropboxusercontent.com/s/llt6sv7y2yurn2v/SPromiseMeSpeedDEBUG.min.js?dl=0"></script>
<script src="/path/to/my/script.js"></script>
</head><body>
...
</body></html>
After
<!doctype HTML>
<html><head>
<script src="https://dl.dropboxusercontent.com/s/llt6sv7y2yurn2v/SPromiseMeSpeedDEBUG.min.js?dl=0" defer=""></script>
<script src="/path/to/my/script.js" defer=""></script>
</head><body>
...
</body></html>
RequireJS and NodeJS Setup
For dropping into either RequireJS or NodeJS, please use the spromisemespeed npm repository, this minified file, or the corresponding source code file. To install via npm, use the following code.
npm install spromisemespeed
After installing via npm, one can use require("spromisemespeed"). Alternatively, one can drop the SPromiseMeSpeed.min.js file into the same directory as their NodeJS script and do require("./SPromiseMeSpeed.min.js"). Both methods are functionally equivalent. Otherwise, if one do not know how to use the command line, save the script corresponding to one's operating system to the directory where the nodejs script will run and use the file manager to run the script (on Windows, it's a double-click).
- fastestsmallesttextencoderdecoder
- Microsoft Windows batch: install-FastestSmallestTextEncoderDecoder-windows.bat
- Bash for Apple MacOS and Linux (e.x. Ubuntu): install-FastestSmallestTextEncoderDecoder-unix.sh
- fastestsmallesttextencoderdecoder-encodeinto
- Microsoft Windows batch: install-FastestSmallestTextEncoderDecoder-encodeInto.bat
- Bash for Apple MacOS and Linux (e.x. Ubuntu): install-FastestSmallestTextEncoderDecoder-encodeInto.sh
API: Differences From Native Promises
SPromiseMeSpeed gives you one and only one new global on the window object: window.SPromise. window.SPromise is the exact same as the native window.Promise as documented by MDN EXCEPT:
- It is called without the
newoperator. - All callbacks are invoked immediately after the promise is resolved without waiting until the next tick.
- It has a
window.SPromise.isPromisemethod you can use to determine if something is a promise. - It is a lot faster than native Chromium promises, even with (surprise, surprise!)
await. - It skims and cuts corners to boost performance by reusing the same reference passed to callbacks as the source.
Example code snippets:
<!DOCTYPE HTML>
<html><head>
<title>SPromise Test Example</title>
<script src="https://dl.dropboxusercontent.com/s/llt6sv7y2yurn2v/SPromiseMeSpeedDEBUG.min.js?dl=0" defer=""></script>
</head><body>
<script>
addEventListener("load", function(){
"use strict";
var SPromise = window.SPromise;
SPromise.race([
SPromise(function(accept){
setTimeout(function(){
accept("SPromise wins this mockup that doesn't actually have any benchmarking practicality");
}, 250);
}),
new Promise(function(accept){ // you can even intermingle native promises with SPromise
setTimeout(function(accept){
accept("Native Promise wins this mockup that doesn't actually have any benchmarking practicality")
}, 250)
})
]).then(function(winner){
document.body.insertAdjacentHTML("beforeend", "<div>Winner:" + winner + "</div>");
});
(async function(){
document.body.insertAdjacentHTML("beforeend", "<div>" + await SPromise(function(accept, reject){
// Don't ask me why synthetic promises are allowed to work with await. I myself am still
// confused, but hey: it works and SPromise even somehow shockingly makes them faster!
setTimeout(function(){
accept("Hello from await!")
}, 750);
}) + "</div>");
})();
})();
</script>
</body></html>
SPromiseMeSpeed.min.js VS SPromiseMeSpeedDEBUG.min.js
The main difference between the two versions is that SPromiseMeSpeedDEBUG is intended for development. It adds many extra type checks and notifies you when the wrong type of object is passed to the SPromise API. For some of the errors, it even gives a suggestion on how to fix them. However, these checks come at a cost: performance. If you have already written your code well enough to not need these checks, then use SPromiseMeSpeed.min.js for even better performance. SPromiseMeSpeed.js will run blind untill it gets done running or it hits a wall and crashes. For example, if you pass something that is not a function to window.SPromise from SPromiseMeSpeedDEBUG.min.js, then it will print a pretty error message saying you passed a wrong type to the function. But with SPromiseMeSpeed.min.js, the console will say something along the lines of 'cannot call null or undefined' or 'null or undefined is not a function.' To use SPromiseMeSpeed without the DEBUG, insert the following alternative code into your <head>:
<script src="https://dl.dropboxusercontent.com/s/i8om2fcz5izdeoj/SPromiseMeSpeed.min.js?dl=0"></script>
RequireJS and ES Module API Documentation
For NodeJS, calling require("SPromiseMeSpeed.min.js") yields the function SPromise.
module.exports = function(handle) {/*...*/};
As for example snippets, observe the way to require modules below.
const SPromise = require("spromisemespeed");
Or, one can use the new and shiny ES6 module importation statements.
// Variation 1
import { SPromise } from "spromisemespeed";
// Variation 2
import * as SPromiseModule from "spromisemespeed";
const SPromise = SPromiseModule.SPromise;
npm Project
This DEBUG build of this project can be found on npm here at this link. The faster unchecked build can be found on npm here at this other link.
Benchmarks
If you are a sensible guy like me, then you shouldn't take my word for the speed of SPromiseMeSpeed. Rather, take the word of these benchmarks:
Casual Promising
Benchmark Code (executed in the console at https://cdnjs.cloudflare.com/ajax/libs/bluebird/2.11.0/bluebird.min.js)
(async function(requestIdleCallback, console, performance){
"use strict";
var resStr = "";
var nativePromise = window.Promise, idleOptions = {timeout: 11};
function test(str, f){
return new nativePromise(function(acc){
var tests=[], tmp=0, SPromise = window.SPromise;
var cycleCount=5, intV=requestIdleCallback(function theeF
