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
490
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
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 Programming
See All in Programming
pylint custom ruleで始めるレビュー自動化
shogoujiie
0
120
iOSエンジニアから始める visionOS アプリ開発
nao_randd
3
130
ファインディの テックブログ爆誕までの軌跡
starfish719
2
1.1k
メンテが命: PHPフレームワークのコンテナ化とアップグレード戦略
shunta27
0
120
Pulsar2 を雰囲気で使ってみよう
anoken
0
240
『GO』アプリ データ基盤のログ収集システムコスト削減
mot_techtalk
0
120
PHPカンファレンス名古屋2025 タスク分解の試行錯誤〜レビュー負荷を下げるために〜
soichi
1
190
AIの力でお手軽Chrome拡張機能作り
taiseiue
0
170
Rails アプリ地図考 Flush Cut
makicamel
1
120
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
110
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
250
『テスト書いた方が開発が早いじゃん』を解き明かす #phpcon_nagoya
o0h
PRO
2
220
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Agile that works and the tools we love
rasmusluckow
328
21k
Fireside Chat
paigeccino
34
3.2k
The World Runs on Bad Software
bkeepers
PRO
67
11k
A designer walks into a library…
pauljervisheath
205
24k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
Side Projects
sachag
452
42k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Scaling GitHub
holman
459
140k
It's Worth the Effort
3n
184
28k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.2k
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