Skip to content

Instantly share code, notes, and snippets.

Avatar
🍵

Shuhei Kagawa shuhei

🍵
View GitHub Profile
@shuhei
shuhei / index.js
Created Mar 2, 2019
Callback of ClientRequest.prototype.end
View index.js
const http = require('http');
const server = http.createServer((req, res) => {
console.log('[server] request');
setTimeout(() => {
res.end('hello');
console.log('[server] response');
}, 3000);
});
server.listen(3000, () => {
@shuhei
shuhei / README.md
Last active Dec 29, 2018
Benchmark of Node.js Histogram Libraries
View README.md
@shuhei
shuhei / optional.js
Created Oct 16, 2018
A test for optimization of optional properties
View optional.js
const a = {
a: 1,
b: null
};
const b = {
a: 2,
b: null
};
const c = {
a: 2,
@shuhei
shuhei / count.js
Created Sep 17, 2018
Count children of a DOM element
View count.js
function countChildren(el) {
const children = el && el.children;
if (!children) {
return 0;
}
let count = 0;
for (let i = 0; i < children.length; i++) {
const child = children[i];
count += 1;
count += countChildren(child);
View symbol.js
const fs = require('fs');
const path = require('path');
class Node {
constructor(start, end, depth) {
this.start = start;
this.end = end;
this.middle = Math.floor((start + end) / 2);
this.items = [];
this.left = null;
@shuhei
shuhei / README.md
Last active Jan 4, 2019
Fixing wrong symbols in Node.js CPU Frame Graphs
View README.md

Fixing wrong symbols in Node.js CPU Frame Graphs

Motivation

I have been generating CPU Flame Graphs of Node.js applications with Linux perf command, and noticed that some of the graphs have suspicious labels. For example, with a sandbox project and Node 10.8.0, I got LazyCompile:*a.renderDOM /opt/app/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:35 where it should have been something related to streams.

For example, the following is a stack trace from perf script that I used to genearte a CPU Flame Graph. 227bbdff955b has a label LazyCompile:*a.renderDOM, but it doesn't make much sense that React.renderDOM was called from _stream_readable.js and parsed JSON in it.

node 22640 56531.256247:   10101010 cpu-clock: 
@shuhei
shuhei / Makefile
Last active Jun 28, 2018
Getting queue stats from libuv with N-API (Node.js v10)
View Makefile
.PHONY: build
configure:
@node-gyp configure
build:
@node-gyp build
run:
@node index.js
@shuhei
shuhei / README.md
Last active Jun 26, 2018
Show gzipped file size
View README.md

gzsize

gzsize shows gzipped file sizes of files in your current working directory.

# For terminal
gzsize

# Markdown table
gzsize -m
@shuhei
shuhei / README.md
Created Jun 25, 2018
Node.js Thread Pool test
View README.md
UV_THREADPOOL=100 node index.js
ab -c 100 -n 1000
htop
@shuhei
shuhei / api.js
Last active Jun 10, 2018
Worker Pool Prototype
View api.js
const url = require('url');
const https = require('https');
const agent = new https.Agent({
keepAlive: true
});
function getAPI(uri) {
return new Promise((resolve, reject) => {
const { protocol, hostname, path } = url.parse(uri);
You can’t perform that action at this time.