Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Faster than C?
Search
Felix Geisendörfer
October 07, 2012
Programming
0
480
Faster than C?
Presentation given at JSConf.eu
Felix Geisendörfer
October 07, 2012
Tweet
Share
More Decks by Felix Geisendörfer
See All by Felix Geisendörfer
tus.io - Resumable File Uploads (Lightning Talk)
felixge
2
740
Programming flying robots with JavaScript
felixge
2
920
Programming flying robots with JavaScript
felixge
0
550
Programming an AR Drone Firmware with JS (de)
felixge
1
590
Faster than C?
felixge
1
1.2k
Flying robots over a 10.000 mile distance with JavaScript.
felixge
0
450
Faster than C?
felixge
1
590
The power of node.js (with quadcopters)
felixge
0
460
Faster than C?
felixge
0
380
Other Decks in Programming
See All in Programming
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
190
CNCF Project の作者が考えている OSS の運営
utam0k
3
310
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
200
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
1
250
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
630
為你自己學 Python
eddie
0
530
情報漏洩させないための設計
kubotak
5
1.3k
20241217 競争力強化とビジネス価値創出への挑戦:モノタロウのシステムモダナイズ、開発組織の進化と今後の展望
monotaro
PRO
0
300
shadcn/uiを使ってReactでの開発を加速させよう!
lef237
0
310
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
370
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
430
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
Featured
See All Featured
Building Your Own Lightsaber
phodgson
104
6.2k
What's in a price? How to price your products and services
michaelherold
244
12k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Docker and Python
trallard
43
3.2k
Building Applications with DynamoDB
mza
93
6.2k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Speed Design
sergeychernyshev
25
740
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
BBQ
matthewcrist
85
9.4k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
4
190
Music & Morning Musume
bryan
46
6.3k
Transcript
Faster than C? Parsing binary data in JavaScript Felix Geisendörfer
7. Oct, 2012 - JSConf.eu
@felixge transloadit.com nodecopter.com
Faster than C?
Sorry about the “title bait”
High performance JavaScript
JavaScript vs C
Good vs Evil
Good Parts vs Evil
Bad Parts vs Evil
early 2010
No MySQL module for node.js early 2010
All we had was NoSQL Libraries early 2010
None
Pure JS / No C/C++
Before Buffers became usable
The Parser was using JavaScript Strings
Node.js Trivia
“ Buffers” used to be called “Blobs”
For 3 min and 15 sec
RIP Blobs ✞
Sun Dec 13 08:39:20 2009 - Sun Dec 13 08:42:45
2009
Anyway
mysql can be done without libmysql
None
No good deed goes unpunished
Sir Isaac Newton
Third Law of Motion
“When a first body exerts a force F1 on a
second body, the second body simultaneously exerts a force F2 = −F1 on the first body. This means that F1 and F2 are equal in magnitude and opposite in direction.”
Third Law of Github
“When a first person pushes a library L1 into a
remote repository, a second person simultaneously starts working on a second library L2 which will be equally awesome, but in a different way.”
<3 Github!
None
0 500 1,000 1,500 mysql−0.9.6 mysql−libmysqlclient−1.5.1 benchmark mbit benchmark mysql−0.9.6
mysql−libmysqlclient−1.5.1
Of course.
libmysql = C
my library = JavaScript
C > JS, right?
But V8!
And Crankshaft!!
Node.js !!!1!
Was I living a lie?
Kind of
V8 / Node = Tools
Performance is not a tool
Performance is hard work & data analysis
0 500 1,000 1,500 mysql−0.9.6 mysql−libmysqlclient−1.5.1mysql−2.0.0−alpha3 benchmark mbit benchmark mysql−0.9.6
mysql−libmysqlclient−1.5.1 mysql−2.0.0−alpha3
Third Law of Github
None
0 1,000 2,000 3,000 4,000 mysql−0.9.6 mysql−libmysqlclient−1.5.1 mysql−2.0.0−alpha3 mariadb−0.1.7 benchmark
mbit benchmark mysql−0.9.6 mysql−libmysqlclient−1.5.1 mysql−2.0.0−alpha3 mariadb−0.1.7
Time to give up?
NEVER!
New Parser
0 2,000 4,000 6,000 mysql2 new−parser benchmark mbit benchmark mysql2
new−parser
Third law of Github?
Endgame
Last bottleneck: Creating JS Objects
Also: MySQL Server saturated
Anyway
How to write fast JS
Does not work
Profiling • Good for spotting small functions with stupid algorithms
performing many iterations • Bad for complex functions with many primitive operations
Taking performance advice from strangers • Good for ideas &
inspiration • But useless when applied cargo-cult style
Does Work
BDD
Behavior Driven Development
Benchmark Driven Development
Benchmark Driven Development • Similar to test driven development •
Use it when performance is an explicit design goal • Benchmark first > benchmark after !
1 function benchmark() { 2 // intentionally empty 3 }
1 while (true) { 2 var start = Date.now(); 3
benchmark(); 4 var duration = Date.now() - start; 5 console.log(duration); 6 }
Benchmark Driven Development • Next step: Implement a tiny part
of your function • Example: Parse headers of MySQL packets • Look at impact, tweak code, repeat
Example Results • try...catch is ok • big switch statement
= bad • function calls = very cheap • buffering is ok
Favorite
1 function parseRow(columns, parser) { 2 var row = {};
3 for (var i = 0; i < columns.length; i++) { 4 row[columns[i].name] = parser.readColumnValue(); 5 } 6 return row; 7 }
Make it faster!
1 var code = 'return {\n'; 2 3 columns.forEach(function(column) {
4 code += '"' + column.name + '":' + 'parser.readColumnValue(),\n'; 5 }); 6 7 code += '};\n'; 8 9 var parseRow = new Function('columns', 'parser', code);
->
1 function parseRow(columns, parser) { 2 return { 3 id
: parser.readColumnValue(), 4 title : parser.readColumnValue(), 5 body : parser.readColumnValue(), 6 created : parser.readColumnValue(), 7 updated : parser.readColumnValue(), 8 }; 9 }
eval = awesome
Data analysis • Produce data points as tab separated values
• Add as many VM/OS metrics as you can get to every line • Do not mix data and analysis !!
Recommended Tools • node benchmark.js | tee results.tsv • R
Programming language (with ggplot2) ! • Makefiles, Image Magick, Skitch
Why?
0 2,000 4,000 6,000 mysql2 new−parser benchmark mbit benchmark mysql2
new−parser
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • •• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 3,000 4,000 5,000 6,000 mysql2 new−parser benchmark mbit benchmark • • mysql2 new−parser
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • •• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 3,000 4,000 5,000 6,000 mysql2 new−parser benchmark mbit benchmark • • mysql2 new−parser Dafuq? Dafuq?
3,000 4,000 5,000 6,000 0 100 200 300 number mbit
benchmark mysql2 new−parser
10 20 30 0 100 200 300 number Heap Total
(MB) benchmark mysql2 new−parser
5 10 15 0 100 200 300 number Heap Used
(MB) benchmark mysql2 new−parser
tl;dr
1. Write a benchmark 2. Write/change a little code 3.
Collect data 4. Find problems 5. Goto 2
Thank you
github.com/felixge/faster-than-c All benchmarks, results and analysis scripts Thank you !
None
None
nodecopter.com Dublin, Oct 20 (Saturday) Brighton, Nov 10 http://tinyurl.com/brcopter