KEMBAR78
Profiling JavaScript Performance | PDF
ProfilingJavaScript
Performance
Welcome
LukeDeWitt
EAST COASTER / HUSBAND / FATHER OF 3 UNDER 5 (TIRED) /
DEVELOPER / DIRECTOR OF WEB SERVICES
Greetings!
JavaScript
• Performance is important
• Everyone loves fast sites
• JS continually redefining itself
• Low barrier to entry
• Simple to learn
• Frameworks
JavaScript
JavaScript
❤ ❤ "
Let’sDiveIn
JavaScript
• The problems are the same, front or back end
JavaScript
• Single threaded
• One “chunk” at a time
ButLuke!
JavaScript
• Single threaded
• One “chunk” at a time
• Avoid blocking code
JavaScript
• Blocking Code
• Render blocking
• Unresponsive UIs
• Accessibility Issues
• Bad UX
CallStack
CallStack
• This is our “single thread”
• Last In, First Out
• Code added when called (stack frame)
• Popped off when “returned”
• Repeat until empty
• Do it again
TheCallStack
call stack
TheCallStack
call stack
function1()
TheCallStack
call stack
function1()
function2()
TheCallStack
call stack
function1()
function2()
function3()
TheCallStack
call stack
function1()
function2()
function3()
function4()
TheCallStack
call stack
function1()
function2()
function3()
function4()
function5()
TheCallStack
call stack
function1()
function2()
function3()
function4()
TheCallStack
call stack
function1()
function2()
function3()
TheCallStack
call stack
function1()
function2()
TheCallStack
call stack
function1()
TheCallStack
call stack
CallStackExample
call stack
CallStackExample
call stack
CallStackExample
call stack
go()
CallStackExample
call stack
go()
CallStackExample
call stack
go()
start()
CallStackExample
call stack
go()
start()
CallStackExample
call stack
go()
start()
andThen()
CallStackExample
call stack
go()
start()
andThen()
CallStackExample
call stack
go()
start()
andThen()
andThenMore()
CallStackExample
call stack
go()
start()
andThen()
andThenMore()
CallStackExample
call stack
go()
start()
andThen()
andThenMore()
andFinally()
CallStackExample
call stack
go()
start()
andThen()
andThenMore()
andFinally()
CallStackExample
call stack
go()
start()
andThen()
andThenMore()
andFinally()
CallStackExample
call stack
go()
start()
andThen()
andThenMore()
andFinally()
console.log()
CallStackExample
call stack
go()
start()
andThen()
andThenMore()
andFinally()
CallStackExample
call stack
go()
start()
andThen()
andThenMore()
CallStackExample
call stack
go()
start()
andThen()
CallStackExample
call stack
go()
start()
CallStackExample
call stack
go()
CallStackExample
call stack
CallStackExample
call stack
node --inspect app.js
node --inspect-brk app.js
ProfileGraph
ProfileGraph
RecursionExample
call stack
RecursionExample
call stack
recursion()
RecursionExample
call stack
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
RecursionExample
call stack
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
recursion()
Example
CallStackExample
call stack
CallStackExample
call stack
CallStackExample
call stack
console.log()
CallStackExample
call stack
console.log()
CallStackExample
call stack
console.log()
cube()
CallStackExample
call stack
console.log()
cube()
CallStackExample
call stack
console.log()
cube()
parseInt()
CallStackExample
call stack
console.log()
cube()
CallStackExample
call stack
console.log()
cube()
CallStackExample
call stack
console.log()
cube()
CallStackExample
call stack
console.log()
cube()
CallStackExample
call stack
console.log()
CallStackExample
call stack
ProfileGraph
JavaScript
JavaScript
• APIs are exposed
• Timers
• Callbacks
• Promises
• Async/Await
• Limits “Blocking” code
• setTimeout === window.setTimeout
EventLoop
• Responsible for pushing items onto the Call
Stack
• Responsible for executing the top most stack
frame
• Runs when the stack is empty
• Also responsible for pulling stack frames from
the queue(s) onto the Call Stack for execution
• Implemented by the browsers / node runtime
• “Web APIs” for browsers
• C++ for node
• Essentially act as threading mechanisms
• Better code execution
• Multiple Queues
ExternalAPIs
JavaScript
call stack
JavaScript
call stack
queue
event loop
external apis
JavaScript
call stack
queue
event loop
function1()
function2()
function3()
external apis
JavaScript
call stack
queue
event loop
function1()
function2()
external apis
JavaScript
call stack
queue
event loop
function1()
external apis
JavaScript
call stack
queue
event loop
external apis
JavaScript
call stack
queue
event loop
external apis
JavaScript
call stack
queue
event loop
external apis
JavaScript
call stack
queue
event loop
external apis
setTimeout(myCB)
JavaScript
call stack
queue
event loop
external apis
setTimeout(myCB)
JavaScript
call stack
queue
event loop
external apis
timer(myCB)
JavaScript
call stack
queue
event loop
external apis
myCB()
JavaScript
call stack
queue
event loop
external apis
myCB()
JavaScript
call stack
queue
event loop
external apis
XHR&PromiseExample
XHR&PromiseGraph
XHR&PromiseGraph(Request)
XHR&PromiseGraph(Request)
XHR&PromiseGraph(Response)
XHR&Async/AwaitExample
XHR&Async/AwaitExample
XHR&Async/AwaitExample
...tooling...
console.log
console.profile
console.profileEnd
Example
Example
Example
ExampleGraph
ExampleGraph
Example
MomentofTruth
OpenSource
https://dewey.codes
ProjectataGlance
• Started building it in 2014
• Domain knowledge / interest existed
• Learning project
• Hard deadline
• … rushed
TheSandbox
CurrentCode
(APICall)
CurrentCode
(APICall)
ProfileGraph
(APICall)
ProfileGraph(APICall)
TimeToActualRequestIndex
ProfileGraph(APICall)
TTARI 22.387ms
ProfileGraph(APICall)
TTARI 22.387ms
TTARISD 0.514ms
ProfileGraph(APICall)
TTARI 22.387ms
TTARISD 0.514ms
CurrentCode
(APICall)
MOAROPENSOURCE!!
UpdatedCode
(APICall)
ProfileGraph(APICallw/Axios)
18.4ms
1.09ms
TTARI
TTARISD
TTARI
Axios 18.4ms
Request 22.387ms
18 Improvemento
o
Wecandobetter…
VanillaCode
(APICall)
ProfileGraph(APICallw/VanillaJS)
15.9ms
0.535ms
TTARI
TTARISD
TTARI
Axios 18.4ms
Request 22.387ms
Vanilla 15.9ms
29 Improvemento
o
TWENTY-NINE
Thankyouforcoming!
redspace.com / T (902) 444.3490 FACEBOOK REDspace
TWITTER @theREDspace
LINKEDIN The REDspaceLUKE DEWITT @whatadewitt
ThankYou!
ThankYou!
Oh, by the way, we’re hiring! https://www.redspace.com/jobs

Profiling JavaScript Performance