Last active
October 24, 2017 19:43
-
-
Save ngerritsen/bc21bc914c3c20a32dfd70dc7af0140a to your computer and use it in GitHub Desktop.
Napa JSON.stringify parallelisation test
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'; | |
const napa = require('napajs'); | |
const NS_PER_SEC = 1e9; | |
const MS_PER_NS = 1e6; | |
const NAPA_WORKERS = 6; | |
const ITEMS = 100000; | |
const data = getData(); | |
run(); | |
runParalellized(); | |
function run() { | |
const timer = getTimer(); | |
data.map(item => JSON.stringify(item)); | |
console.log(`Without napa took ${timer.elapsed()}ms`); | |
} | |
function runParalellized() { | |
const zone = napa.zone.create('zone', { workers: NAPA_WORKERS }); | |
const chunks = chunk(data, NAPA_WORKERS); | |
const timer = getTimer(); | |
const promises = chunks.map(chunk => { | |
return zone.execute(chunk => chunk.map(item => JSON.stringify(item)), [chunk]); | |
}); | |
Promise.all(promises).then((resultChunks) => { | |
let result = []; | |
for (const resultChunk of resultChunks) { | |
result = result.concat(resultChunk.value); | |
} | |
console.log(`With napa took ${timer.elapsed()}ms`); | |
}); | |
} | |
function getTimer() { | |
const time = process.hrtime(); | |
return { | |
elapsed() { | |
const [seconds, nanoseconds] = process.hrtime(time); | |
return ((seconds * NS_PER_SEC) + nanoseconds) / MS_PER_NS; | |
} | |
} | |
} | |
function getData() { | |
const data = []; | |
for (let i = 0; i < ITEMS; i++) { | |
data.push({ | |
id: Math.random(), | |
username: 'John Doe', | |
age: 29 | |
}); | |
} | |
return data; | |
} | |
function chunk(items, chunkCount) { | |
const itemCount = items.length; | |
const chunkSize = Math.ceil(itemCount / chunkCount); | |
const chunks = []; | |
for (let i = 0; i < itemCount; i += chunkSize) { | |
chunks.push(items.slice(i, i + chunkSize)); | |
} | |
return chunks; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment