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
RSNA2024振り返り
nanachi
0
580
リーダブルテストコード 〜メンテナンスしやすい テストコードを作成する方法を考える〜 #DevSumi #DevSumiB / Readable test code
nihonbuson
11
7.2k
データの品質が低いと何が困るのか
kzykmyzw
6
1.1k
PHPカンファレンス名古屋-テックリードの経験から学んだ設計の教訓
hayatokudou
2
260
『衛星データ利用の方々にとって近いようで触れる機会のなさそうな小話 ~ 衛星搭載ソフトウェアと衛星運用ソフトウェア (実物) を動かしながらわいわいする編 ~』 @日本衛星データコミニティ勉強会
meltingrabbit
0
140
オブザーバビリティの観点でみるAWS / AWS from observability perspective
ymotongpoo
8
1.5k
エンジニアが加速させるプロダクトディスカバリー 〜最速で価値ある機能を見つける方法〜 / product discovery accelerated by engineers
rince
4
320
2024.02.19 W&B AIエージェントLT会 / AIエージェントが業務を代行するための計画と実行 / Algomatic 宮脇
smiyawaki0820
13
3.3k
表現を育てる
kiyou77
1
210
Swiftの “private” を テストする / Testing Swift "private"
yutailang0119
0
130
OpenID Connect for Identity Assurance の概要と翻訳版のご紹介 / 20250219-BizDay17-OIDC4IDA-Intro
oidfj
0
270
AndroidXR 開発ツールごとの できることできないこと
donabe3
0
130
Featured
See All Featured
Writing Fast Ruby
sferik
628
61k
Faster Mobile Websites
deanohume
306
31k
Rails Girls Zürich Keynote
gr2m
94
13k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
174
51k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
The Language of Interfaces
destraynor
156
24k
Bootstrapping a Software Product
garrettdimon
PRO
306
110k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Making Projects Easy
brettharned
116
6k
KATA
mclloyd
29
14k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
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 !