Web worker data copying performance

Benchmark created by Ramesh Nair on


Description

Compare the time taken to copy large Typed arrays from the main thread to a WebWorker and back.

Note: Only browsers which support StructureCloning will be able to copy Typed arrays.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script type="javascript/worker" id="workerFunc">
  self.onmessage = function(e) {
    postMessage(e.data); // Pass through
  };
</script>

<script type="text/javascript">
$(function() {
console.log("Creating data arrays");

var data32mb = new Uint8Array(1024 * 1024 * 32);
var data1mb = new Uint8Array(1024 * 1024);
for (var i=0; i<data32mb.length; i++) {
  data32mb[i] = parseInt(Math.random()* 256);
  if (data1mb.length > i) {
    data1mb[i] = data32mb[i];
  }
}
window.data32mb = data32mb;
window.data1mb = data1mb;

console.log("Creating web worker");

window.URL = window.URL || window.webkitURL || null;
var script = document.querySelector('#workerFunc');
var blob = new Blob([script.textContent]);
window.testWorker = new Worker(window.URL.createObjectURL(blob));

console.log("Setup complete");
});
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
32MB
// async test
window.testWorker.onmessage = function(e) {
  deferred.resolve();
};
window.testWorker.postMessage(window.data32mb);
 
ready
1MB
// async test
window.testWorker.onmessage = function(e) {
  deferred.resolve();
};
window.testWorker.postMessage(window.data1mb);
 
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Ramesh Nair on