Created
September 15, 2022 03:09
-
-
Save nermolov/c842d08c9ddce9107e58610d9d0dfd8e to your computer and use it in GitHub Desktop.
node.js v16 array iterator blocking test
This file contains 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
#!/usr/bin/env fish | |
# Once node server is running, run test by endpoint name: | |
# ./curl.fish long1 | |
function getTime | |
printf (cat $argv | awk '{ print $1 }') | |
end | |
while true | |
echo :: long call start | |
command time -ho longTime curl -s localhost:3000/$argv > /dev/null & | |
set longPid $last_pid | |
sleep 0.05 | |
echo :: hello call start | |
command time -ho shortTime curl -s localhost:3000/hello > /dev/null | |
echo :: hello call finished in (getTime shortTime) + 0.05s | |
wait $longPid | |
echo :: long call finished in (getTime longTime) | |
echo | |
sleep 1 | |
end |
This file contains 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
// node v16.17.0 amd64 under macOS Rosetta | |
const express = require('express') | |
const app = express() | |
const port = 3000 | |
app.get('/hello', (req, res) => { | |
res.send('Hello World!') | |
}) | |
const n = 100000000 | |
const arr = Array(n).fill(Math.random()) | |
// blocks -> blocks event loop | |
// ~0.12s, blocks | |
app.get('/long1', (req, res) => { | |
let sum = 0 | |
for (let i = 0; i < arr.length; i++) { | |
sum += arr[i] | |
} | |
res.send(sum.toString()) | |
}) | |
// ~0.47s, blocks | |
app.get('/long2', (req, res) => { | |
let sum = 0 | |
for (const el of arr) { | |
sum += el | |
} | |
res.send(sum.toString()) | |
}) | |
// ~1.89s, blocks | |
app.get('/long3', (req, res) => { | |
let sum = 0 | |
arr.forEach(el => { | |
sum += el | |
}) | |
res.send(sum.toString()) | |
}) | |
// ~0.9s, blocks | |
app.get('/long4', (req, res) => { | |
const sum = arr.reduce((prev, el) => { | |
prev += el | |
}) | |
res.send(sum.toString()) | |
}) | |
app.listen(port, () => { | |
console.log(`Example app listening on port ${port}`) | |
}) |
This file contains 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
taylor@Taylors-MacBook-Pro ~/p/t/exp-test> ./curl.fish long4 | |
:: long call start | |
:: hello call start | |
:: hello call finished in 0.87s + 0.05s | |
:: long call finished in 0.91s | |
:: long call start | |
:: hello call start | |
:: hello call finished in 0.88s + 0.05s | |
:: long call finished in 0.93s | |
:: long call start | |
:: hello call start | |
:: hello call finished in 0.85s + 0.05s | |
:: long call finished in 0.90s | |
:: long call start | |
:: hello call start | |
:: hello call finished in 0.86s + 0.05s | |
:: long call finished in 0.92s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment