Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Let’s start killing a Processor with single thread
//sync processfunction slowSquare(n){ var i = 0; while (++i < n * n) {}}
slowSquare(100000)slowSquare(100000)
$time node app.js28.04 secs
//async processfunction slowSquareAsync(){ setTimeout(function(args) { let i = 0; let n = 100000 while (++i < n * n) {} }, 1) }
slowSquareAsync()slowSquareAsync()
$time node app.jsWaiting too long then I aborted (ctrl+c) after wait about 4mins
And why my laptop coming hot? :D
After read few article I found this
Then I tried create very small module, I called Iyem.js , iyem meaning is maid in Bahasa
I will try same code but using Iyem.js
let iyem = require('iyem')
let slowSquareThread = iyem.create(function(){ var i = 0; var n = 100000; while (++i < n * n) {} $.finish(i); })slowSquareThread.start()slowSquareThread.onFinish(function(result,err){ console.log(result,err) })
function slowSquare(n){ var i = 0; while (++i < n * n) {}}
slowSquare(100000)
$time node app.js13.2 secs
Wow, cool right? , let’s try more complex multi thread with communication between thread (Pub Sub)
let iyem = require('iyem')
let importDataReportWorker = iyem.create(function(){ const parse = require('parse-csv') const fs = require('fs') const reconcile = require('./Reconcile') parse(fs.readFileSync("./200MB.csv", "utf-8"),function(err,report) { $.sub("GET_TRX_SETTELMENT_COLLECTION_DONE",function(dataTrx){ var result = reconcile(report,dataTrx) $.finish(result) }) })})
let getTrxCollection = iyem.create(function(){ const db = require('./db') db.find({transaction_date:"2017-10-10"},function(err,collection){ if(err) $.error(err) $.pub("GET_TRX_SETTELMENT_COLLECTION_DONE",collection) $.finish() })})
getTrxCollection.start()importDataReportWorker.start().onFinish(function(err,result){ console.log("DONE") console.log(result)})
this is just experimental, Please give me your feedback and dont forget to clap or share
If you learned something, click the 💚 to help others find this article.
Very simple implementation multi thread in Node.js was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.