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
Front-end performance - Making lunr.js fast
Search
Oliver Nightingale
May 02, 2013
Technology
2
1.4k
Front-end performance - Making lunr.js fast
Three stories about making lunr.js searching fast
Oliver Nightingale
May 02, 2013
Tweet
Share
More Decks by Oliver Nightingale
See All by Oliver Nightingale
d3_meetup_20120130.pdf
olivernn
1
61
D3 Geo Visualisations
olivernn
3
2k
Other Decks in Technology
See All in Technology
Git in Team
kawaguti
PRO
3
340
20251014_Pythonを実務で徹底的に使いこなした話
ippei0923
0
120
生成AIとM5Stack / M5 Japan Tour 2025 Autumn 東京
you
PRO
0
240
なぜAWSを活かしきれないのか?技術と組織への処方箋
nrinetcom
PRO
2
600
【Kaigi on Rails 事後勉強会LT】MeはどうしてGirlsに? 私とRubyを繋いだRail(s)
joyfrommasara
0
230
AWS Top Engineer、浮いてませんか? / As an AWS Top Engineer, Are You Out of Place?
yuj1osm
2
210
AIツールでどこまでデザインを忠実に実装できるのか
oikon48
6
3.2k
実装で解き明かす並行処理の歴史
zozotech
PRO
1
690
Codexとも仲良く。CodeRabbit CLIの紹介
moongift
PRO
0
110
AI駆動開発を推進するためにサービス開発チームで 取り組んでいること
noayaoshiro
0
250
やる気のない自分との向き合い方/How to Deal with Your Unmotivated Self
sanogemaru
0
470
Escaping_the_Kraken_-_October_2025.pdf
mdalmijn
0
160
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Designing for Performance
lara
610
69k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
54
3k
Building Adaptive Systems
keathley
43
2.8k
The Pragmatic Product Professional
lauravandoore
36
6.9k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
The World Runs on Bad Software
bkeepers
PRO
71
11k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
900
Scaling GitHub
holman
463
140k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.7k
Automating Front-end Workflow
addyosmani
1371
200k
How to train your dragon (web standard)
notwaldorf
96
6.3k
Transcript
FRONT-END PERFORMANCE MAKING LUNR.JS FAST Thursday, 2 May 13
100% CPU BOUND Thursday, 2 May 13
PROFILE TOOL Thursday, 2 May 13
BENCHMARK ๏ CONSOLE.TIME(‘ZOMG SLOW’) ๏ JSPERF.COM ๏ BENCHMARK.JS BEFORE &
AFTER Thursday, 2 May 13
DO LESS GO QUICKER Thursday, 2 May 13
lunr.Vector = function (elements) { this.elements = elements for (var
i = 0; i < elements.length; i++) { if ( !(i in this.elements) ) this.elements[i] = 0 } } BEFORE Thursday, 2 May 13
lunr.Vector = function (elements) { this.elements = elements } AFTER
Thursday, 2 May 13
var zeros = [0] var zeroFillArray = function (length) {
while (zeros.length < length) { zeros = zeros.concat(zeros) } return zeros.slice(0, length) } ELSEWHERE Thursday, 2 May 13
RESULTS 10 100 1000 10000 100000 1000000 NON ZERO FILLED
ARRAY ZERO FILLED ARRAY 774,676 ops/s 3,234 ops/s http://jsperf.com/lunr-vector-init Thursday, 2 May 13
LESS RECURSION FUNCTION OVER-HEAD Thursday, 2 May 13
EVIDENCE Thursday, 2 May 13
lunr.TokenStore.prototype.getNode = function (token, root) { var root = root
|| this.root, key = token[0], rest = token.slice(1) if (!(key in root)) return {} if (rest.length === 0) { return root[key] } else { return this.getNode(rest, root[key]) } } BEFORE Thursday, 2 May 13
lunr.TokenStore.prototype.getNode = function (token) { if (!token) return {} var
node = this.root for (var i = 0; i < token.length; i++) { if (!(token[i] in node)) return {} node = node[token[i]] } return node } AFTER Thursday, 2 May 13
RESULTS 40000 80000 120000 160000 RECURSIVE ITERATIVE 152,008 ops/s 103,070
ops/s http://jsperf.com/lunr-tokenstore-getnode/2 Thursday, 2 May 13
BIG OHHH CHOOSING THE RIGHT ALGORITHM Thursday, 2 May 13
lunr.SortedSet = function () { this.length = 0 this.elements =
[] } SIMPLE DATA STRUCTURE Thursday, 2 May 13
OPERATIONS ๏ ADD(ELEM) ๏ INDEX_OF(ELEM) SORTED SET Thursday, 2 May
13
lunr.SortedSet.prototype.add = function (elem) { if (~this.elements.indexOf(elem)) return this.elements.push(elem) this.length++
this.elements.sort() } BEFORE Thursday, 2 May 13
SORT O(n log n) Thursday, 2 May 13
INDEX_OF O(n) Thursday, 2 May 13
lunr.SortedSet.prototype.add = function (elem) { if (~this.indexOf(elem)) return this.elements.splice(this.locationFor(elem), 0,
elem) this.length = this.elements.length } AFTER Thursday, 2 May 13
BINARY SEARCH O(log n) Thursday, 2 May 13
lunr.SortedSet.prototype.indexOf = function (elem, start, end) { var start =
start || 0, end = end || this.elements.length, sectionLength = end - start, pivot = start + Math.floor(sectionLength / 2), pivotElem = this.elements[pivot] if (sectionLength <= 1) { if (pivotElem === elem) { return pivot } else { return -1 } } if (pivotElem < elem) return this.indexOf(elem, pivot, end) if (pivotElem > elem) return this.indexOf(elem, start, pivot) if (pivotElem === elem) return pivot } ELSEWHERE Thursday, 2 May 13
lunr.SortedSet.prototype.locationFor = function (elem, start, end) { var start =
start || 0, end = end || this.elements.length, sectionLength = end - start, pivot = start + Math.floor(sectionLength / 2), pivotElem = this.elements[pivot] if (sectionLength <= 1) { if (pivotElem > elem) return pivot if (pivotElem < elem) return pivot + 1 } if (pivotElem < elem) return this.locationFor(elem, pivot, end) if (pivotElem > elem) return this.locationFor(elem, start, pivot) } MORE ELSEWHERE Thursday, 2 May 13
RESULTS 100 10000 1000000 LOW VALUE HIGH VALUE NON EXISTANT
INDEX_OF BINARY_SEARCH INDEX_OF BINARY_SEARCH INDEX_OF BINARY_SEARCH 4,818,044 ops/s 5,521,920 ops/s 4,768,333 ops/s 14,445 ops/s 23,150 ops/s 1,697,089 ops/s http://jsperf.com/lunr-sortedset-indexof Thursday, 2 May 13
END USER PERF THIS IS WHAT REALLY MATTERS Thursday, 2
May 13
RESULTS 75 150 225 300 0.2.0 0.4.0 38.477 ms 240.865
ms lunr.Index.prototype.search Thursday, 2 May 13