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? Parsing binary data in JavaScript.
Search
Felix Geisendörfer
October 07, 2012
Technology
3
3.8k
Faster than C? Parsing binary data in JavaScript.
Talk given at JSConf.eu 2012.
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
750
Programming flying robots with JavaScript
felixge
2
920
Programming flying robots with JavaScript
felixge
0
560
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
600
The power of node.js (with quadcopters)
felixge
0
470
Faster than C?
felixge
0
380
Other Decks in Technology
See All in Technology
エンジニアのためのドキュメント力基礎講座〜構造化思考から始めよう〜(2025/02/15jbug広島#15発表資料)
yasuoyasuo
17
6.7k
偶然 × 行動で人生の可能性を広げよう / Serendipity × Action: Discover Your Possibilities
ar_tama
1
1.1k
レビューを増やしつつ 高評価維持するテクニック
tsuzuki817
1
710
「海外登壇」という 選択肢を与えるために 〜Gophers EX
logica0419
0
700
The Future of SEO: The Impact of AI on Search
badams
0
190
30分でわかる『アジャイルデータモデリング』
hanon52_
9
2.7k
Classmethod AI Talks(CATs) #17 司会進行スライド(2025.02.19) / classmethod-ai-talks-aka-cats_moderator-slides_vol17_2025-02-19
shinyaa31
0
120
Classmethod AI Talks(CATs) #16 司会進行スライド(2025.02.12) / classmethod-ai-talks-aka-cats_moderator-slides_vol16_2025-02-12
shinyaa31
0
110
TAMとre:Capセキュリティ編 〜拡張脅威検出デモを添えて〜
fujiihda
2
240
地方拠点で エンジニアリングマネージャーってできるの? 〜地方という制約を楽しむオーナーシップとコミュニティ作り〜
1coin
1
230
ホワイトボードチャレンジ 説明&実行資料
ichimichi
0
130
利用終了したドメイン名の最強終活〜観測環境を育てて、分析・供養している件〜 / The Ultimate End-of-Life Preparation for Discontinued Domain Names
nttcom
2
190
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Building Adaptive Systems
keathley
40
2.4k
Thoughts on Productivity
jonyablonski
69
4.5k
Designing for Performance
lara
604
68k
BBQ
matthewcrist
87
9.5k
Designing for humans not robots
tammielis
250
25k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
240
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
Automating Front-end Workflow
addyosmani
1368
200k
Transcript
Faster than C? Parsing binary data in JavaScript Felix Geisendörfer
7. Oct, 2012 - JSConf.eu
@felixge
transloadit.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
Collect data -> Analyze it -> Find problems -> Tweak
the code -> Repeat
Thank you
github.com/felixge/faster-than-c All benchmarks, results and analysis scripts Thank you !