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
const obj = { | |
a: { | |
b: { | |
d: 2 | |
}, | |
c: { | |
e: 5 | |
} | |
} | |
}; |
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
const { parentPort } = require('worker_threads'); | |
parentPort.on('message', (buffer) => { | |
const int8Arr = new Int8Array(buffer); | |
int8Arr[0] = 99; | |
parentPort.postMessage('change value ok'); | |
}); |
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
const { Worker } = require('worker_threads'); | |
const path = require('path'); | |
const buffer = new SharedArrayBuffer(1); | |
const worker = new Worker(path.resolve('./worker.js')); | |
worker.on('message', () => { | |
// check buffer was changed ? | |
const int8Array = new Int8Array(buffer); | |
console.log(`main thread buffer: ${int8Array[0]}`) |
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
const { parentPort } = require('worker_threads'); | |
const sha256 = require('sha256'); | |
parentPort.on('message', (count) => { | |
const shaArray = Array.from(Array(count)).map(() => sha256(Math.random().toString())) | |
parentPort.postMessage(shaArray); | |
}); |
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
const { Worker } = require('worker_threads'); | |
const path = require('path'); | |
const arrayCount = 2000000; | |
const threadCount = 4; | |
let initialTime = new Date(); | |
for (let i = 0; i < threadCount; i++) { | |
const worker = new Worker(path.resolve('./worker.js')); | |
worker.on('message', () => { |
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
const sha256 = require('sha256'); | |
const arrayCount = 2000000; | |
console.time('single thread'); | |
Array.from(Array(arrayCount)).map(() => sha256(Math.random().toString())); | |
console.timeEnd('single thread'); |
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
const { parentPort } = require('worker_threads'); | |
parentPort.on('message', (message) => { | |
console.log(`worker get message: ${message}`); | |
parentPort.postMessage('got it!'); | |
}); |
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
const { Worker } = require('worker_threads'); | |
const path = require('path'); | |
const worker1 = new Worker(path.resolve('./worker.js')); | |
worker1.on('message', (message) => { | |
console.log(`main thread get message: ${message}`); | |
}); | |
worker1.postMessage('hello world'); |
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
const { parentPort, workerData } = require('worker_threads'); | |
console.log(workerData); | |
let b = 0; | |
for (let i = 0; i < 1000; i += workerData.base) { | |
b += i; | |
} | |
parentPort.postMessage(b); |
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
const { Worker } = require('worker_threads'); | |
const path = require('path'); | |
const worker1 = new Worker(path.resolve('./worker.js'), { workerData: { base: 1 } }); | |
const worker2 = new Worker(path.resolve('./worker.js'), { workerData: { base: 10 } }); | |
worker1.on('message', (message) => { | |
console.log(`worker1 message: ${message}`); | |
}); |
NewerOlder