Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This file demonstrate using web worker to compress the data at frontend side (step 4)
* Called when worker will be initialized
* @param worker
function workerInitialized (worker) {
// This will be called when worker finished compressing data. you can access data at
worker.onmessage = function (e) {
// Lets wait for the document to be ready, so that we will enought data to send
$(document).ready(function () {
// Let's create a lot of data to test the compression
var aLongString = '',
for (var i = 0; i < 1000; i++) {
aLongString = aLongString + JSON.stringify(window.performance);
dataToSend = {
name: 'Hemkaran Raghav',
value: aLongString
// Since data is ready, will be send two calls:
// 1. One call without compressing the data
// 2. Second call after worker will compress our data
id: 1,
data: dataToSend,
action: 'compress'
// Used by workerInitialized to send different data
function sendData (data) {
// This url is created using Postman (
// mock server, to check how to create a mock server
// check here:
url: '',
type: 'POST',
data: data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.