Created
January 3, 2018 19:42
-
-
Save nicholasaiello/0a9bb32719c1b64774a8a568ab8c2be8 to your computer and use it in GitHub Desktop.
Web Worker Transfers (http://jsbench.github.io/#0a9bb32719c1b64774a8a568ab8c2be8) #jsbench #jsperf
This file contains 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>Web Worker Transfers</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 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 () { | |
var parseCode = "self.onmessage = function (e){var s = e.data.string; self.postMessage(s); };"; | |
var passCode = "self.onmessage = function (e) { var parts = e.data.split(','); self.postMessage(parts[0]); }"; | |
var transferCode = "self.onmessage = function (e) { self.postMessage(e.data, [e.data]); }"; | |
var parseBlob = new Blob([parseCode], { | |
type: 'text/javascript' | |
}); | |
var passBlob = new Blob([passCode], { | |
type: 'text/javascript' | |
}); | |
var transferBlob = new Blob([transferCode], { | |
type: 'text/javascript' | |
}); | |
var aWorker = new Worker(window.URL.createObjectURL(parseBlob)); | |
var bWorker = new Worker(window.URL.createObjectURL(passBlob)); | |
var cWorker = new Worker(window.URL.createObjectURL(transferBlob)); | |
// warm up the transfer based worker | |
var transferObject = new ArrayBuffer(32); | |
cWorker.postMessage(transferObject, [transferObject]); | |
}; | |
Benchmark.prototype.teardown = function () { | |
aWorker.terminate(); | |
bWorker.terminate(); | |
cWorker.terminate(); | |
}; | |
suite.add("var jsonObject = { string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello' };", function () { | |
var jsonObject = { string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello' }; | |
aWorker.postMessage(jsonObject); | |
}); | |
suite.add("var stringObject = { string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello' };", function () { | |
var stringObject = { string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello', string: 'hello' }; | |
bWorker.postMessage(JSON.stringify(stringObject)); | |
}); | |
suite.add("var transferObject = new ArrayBuffer(32);", function () { | |
var transferObject = new ArrayBuffer(32); | |
cWorker.postMessage(transferObject, [transferObject]); | |
}); | |
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("Web Worker Transfers"); | |
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