This Lesson Taught Me How To Do Better Benchmarks

ThePrimeagen · Intermediate ·🌐 Frontend Engineering ·3y ago

Key Takeaways

ThePrimeagen conducts an experiment to determine the fastest left pad implementation in JavaScript, using micro benchmarks and Apache benchmarking to compare different implementations, including native methods and custom solutions.

Full Transcript

left pad an 11 line piece of code broke the internet and just recently had a second incident that involves me so I might have made a mistake where I thought I could program a faster version of left pad live and the results uh were not faster it goes to here left pad with this one okay so it looks like it's looked like mine is slower you hate to see it at first it inspired a meme then Ember hex made an actually fast version then the creator of crab laying through his hat in the ring even faster than Embers it might is still so slow a Renaissance of left pad happened and other people were joining in and then a competition was born who had the fastest left pad submissions were made more submissions were made then corrected and meanwhile I was hating life on YouTube I've officially cured of imposter syndrome too spicy it even stack Overflow had their own bespoke solution and it was recursive not only that but there's also a native left pad solution provided in modern JavaScript and we need to figure out which left pad is the fastest and find out do micro benchmarks lie yes they do so this competition has been about micro benchmarks but here's the thing is that some of you might not even know what micro benchmarks are well they're as simple as this you write a function you wish to test you've run it a bunch of times taking a bunch of time differentials and you see which function performs better than the other you have different implementations and you hope one of them is going to perform way faster then you've run it a whole bunch and you see that some of them are much slower say mine or the stack Overflow one and some of them are way faster like Embers or the native one or the special case one that I also have in a right and then you call that a day right you say hey my code is fast well here's the deal it's actually not fast you actually don't even know if they're fast in fact you just might be lied to but why well first off you're probably running them on your computer which if I just go P grep node I don't even know dude I must have a Bitcoin miner on my machine I do have Zig running hey calm down ladies and our choosers okay I'm taken tests also often hide implementation details such as garbage collection when you run these micro benchmarks they don't necessarily trigger all the things that could happen when you're running in production and finally it depends on what you're actually doing in your function the thing about JavaScript is that strings aren't strings they're actually rope data structures and there's a lot of optimizations around concatenation and depending with a heavier penalization of iterating over each character so that means if we're just appending a bunch of strings together it may look good on paper but when you actually use the string to do something say send it across the internet or print it out it has to iterate over a more complicated structure to get that string back out so you pay none of the cost of using your string you only pay the cost of producing it which could be cheaper thus giving you misleading results so how do we determine which left pad is actually faster well first I created a server set up Apache benchmark script and ensure proper sleep between each and manually execute garbage collection on the server if no requests have happened within one second send 50 million requests that took about 36 hours to complete I'd personally like to thank lenode for giving me an instance to run this on and not shutting me down after 50 million requests thanks Andrew create a parser to parse the Apache Benchmark results and calculate the average of medians import results into Google spreadsheets charts before we dive into the charts let me kind of explain what the data is you're looking at so each one of these points is the average of the median so what I did is I do like twelve and a half thousand requests and I'd take the median time of that and then I do it again and I do it again and I do it again and after a bunch of those requests I'd have a several amount of medians and then I take the average of those so each one of these represents somewhere between 50 to 100 000 requests each and then of course down here I just simply summed all those numbers across every size of requests and then I ordered these columns according to who had the largest sum all right so this chart is probably the most important chart to look at is the summed of the median averages on the y-axis we have milliseconds and on the x-axis we have the different left pads summed averages of the medians so worst place came in as the stack Overflow tail recursive version which I'm a bit surprised by then came left pad commanding a strong second worst place then look at this I'm talking about you Tom it hurt but the fastest really came down to left pad Travi and Native they're really within a margin of error of each other so what is the takeaway of this the takeaway is use native methods they're way better you don't have to be clever you don't have the right code that looks like that whatever what is this and the best part is is that as new techniques come out as things happen over time you don't have to make these weird changes to your code where you did these weird incantations to figure out what codes actually faster than the other code instead you know the platform is motivated to make a fast product so stick with syntax or vendor implemented options where possible and you will mostly get the best performance but if you ever feel spicy and you want to do power of two string concatenation and bit shifting well go ahead give it a try which is funny because that's actually how string dot repeat works is power of two string concatenation and bit shifting in V8 I don't I have no idea why that's fast I literally have no idea hey if you like this press the like button press subscribe come on give me some signals okay if you want more of this send me the signals make comments do all the stupid things you know you're supposed to do already okay because YouTube highly favors that so if you don't do it it makes me feel like this is a failure and then I feel like a failure and then my wife thinks I'm a failure don't do it to the wife of Jen this is for you Travi

Original Description

This benchmark was HORRIBLE but once I pushed it to the server, something WILD happened instead. ### Twitch Everything is built live on twitch Twitch : https://bit.ly/3xhFO3E Discord: discord.gg/ThePrimeagen Spotify DevHour: https://open.spotify.com/show/4MLaDrQcQ5mi3rsnvWkwPt ### Editor All my videos are edited by Flip. Give him a follow! https://twitter.com/flipmediaprod Join this channel to get access to perks: https://www.youtube.com/channel/UC8ENHE5xdFSwx71u3fDH5Xw/join ### Links Linode: https://linode.com/prime https://discord.gg/ThePrimeagen Twitch: https://twitch.tv/ThePrimeagen Insta: https://instagram.com/ThePrimeagen Twitter: https://twitter.com/ThePrimeagen VimRC & i3: https://github.com/ThePrimeagen/.dotfiles Keyboard 15% off https://bit.ly/Prime360 USE CODE PRIME360 #coding #neovim #typescript #programming #vim #softwareengineering #codinglife #webdesign #webdevelopment #webdev #javascript #rustlang #rust #twitch #twitchstreamer #programmerhumor #codinghumor #software #softwareengineer #softwaredeveloper #softwaredevelopment
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from ThePrimeagen · ThePrimeagen · 0 of 60

← Previous Next →
1 Two Simple Steps to go from IDE to Vim
Two Simple Steps to go from IDE to Vim
ThePrimeagen
2 Git's Best And Most Unknown Feature
Git's Best And Most Unknown Feature
ThePrimeagen
3 My Developer Workflow - How I use i3, tmux, and vim
My Developer Workflow - How I use i3, tmux, and vim
ThePrimeagen
4 Github Copilot MAKES A CLI GAME IN GOLANG FROM SCRATCH?!?!
Github Copilot MAKES A CLI GAME IN GOLANG FROM SCRATCH?!?!
ThePrimeagen
5 I made the greatest tool ever! | tmux & cht.sh & fzf
I made the greatest tool ever! | tmux & cht.sh & fzf
ThePrimeagen
6 Is TypeScript (NodeJS) Faster than Go?? |  A server comparison
Is TypeScript (NodeJS) Faster than Go?? | A server comparison
ThePrimeagen
7 How to make TypeScript Blazingly Fast (JavaScript/NodeJS)
How to make TypeScript Blazingly Fast (JavaScript/NodeJS)
ThePrimeagen
8 Learn Vim in Less than 2 Minutes
Learn Vim in Less than 2 Minutes
ThePrimeagen
9 The biggest lie about programming?  REACTING to tech twitter
The biggest lie about programming? REACTING to tech twitter
ThePrimeagen
10 Rust or Go for my next project? WHAT TO CHOOSE? (as a senior intern engineer)
Rust or Go for my next project? WHAT TO CHOOSE? (as a senior intern engineer)
ThePrimeagen
11 Go is faster than Rust??!  Go vs Rust vs TypeScript Servers (as a scientist)
Go is faster than Rust??! Go vs Rust vs TypeScript Servers (as a scientist)
ThePrimeagen
12 You only Git Merge?!? feat Theo : DevHour #1
You only Git Merge?!? feat Theo : DevHour #1
ThePrimeagen
13 YOU DONT UNIT TEST??? DevHour #1 Theo
YOU DONT UNIT TEST??? DevHour #1 Theo
ThePrimeagen
14 Tech TikTok - Is this really programming? | React
Tech TikTok - Is this really programming? | React
ThePrimeagen
15 I am dying
I am dying
ThePrimeagen
16 Comparing Rust (Yew) vs TypeScript (React) NodeJS | SSR times
Comparing Rust (Yew) vs TypeScript (React) NodeJS | SSR times
ThePrimeagen
17 Why I use Vim in 2022
Why I use Vim in 2022
ThePrimeagen
18 How programming feels
How programming feels
ThePrimeagen
19 Why I Quit Netflix
Why I Quit Netflix
ThePrimeagen
20 Programming on TikTok
Programming on TikTok
ThePrimeagen
21 If I could give advice to myself when starting as a software engineer
If I could give advice to myself when starting as a software engineer
ThePrimeagen
22 How am I suppose to get a job as a software engineer?
How am I suppose to get a job as a software engineer?
ThePrimeagen
23 Why do I feel burnt out? (software engineering)
Why do I feel burnt out? (software engineering)
ThePrimeagen
24 I am done with vim
I am done with vim
ThePrimeagen
25 Maintaining Motivation as a Software Engineer
Maintaining Motivation as a Software Engineer
ThePrimeagen
26 Bun (TypeScript) is faster than Rust!?!
Bun (TypeScript) is faster than Rust!?!
ThePrimeagen
27 Go is blazingly faster than Zig?
Go is blazingly faster than Zig?
ThePrimeagen
28 Facebook Recommends Rust?
Facebook Recommends Rust?
ThePrimeagen
29 5 Things I Hate About JavaScript
5 Things I Hate About JavaScript
ThePrimeagen
30 Why I Make Content
Why I Make Content
ThePrimeagen
31 Is ADHD Bad For Programming?
Is ADHD Bad For Programming?
ThePrimeagen
32 Vim As You Editor - Advanced Motions P1
Vim As You Editor - Advanced Motions P1
ThePrimeagen
33 How I Would Get My First Job If I Started Over
How I Would Get My First Job If I Started Over
ThePrimeagen
34 Vim as your editor - Advanced Motions P2
Vim as your editor - Advanced Motions P2
ThePrimeagen
35 My Best Decision Professionally
My Best Decision Professionally
ThePrimeagen
36 Should you use RUST as your FIRST programming language?
Should you use RUST as your FIRST programming language?
ThePrimeagen
37 How I Made JavaScript BLAZINGLY FAST
How I Made JavaScript BLAZINGLY FAST
ThePrimeagen
38 Is JSON Blazingly Fast or...?
Is JSON Blazingly Fast or...?
ThePrimeagen
39 Vim As Your Editor - Introduction
Vim As Your Editor - Introduction
ThePrimeagen
40 Vim As Your Editor - Horizontal
Vim As Your Editor - Horizontal
ThePrimeagen
41 Astro IS SO GOOD
Astro IS SO GOOD
ThePrimeagen
42 STOP LYING, The truth about Software Engineering
STOP LYING, The truth about Software Engineering
ThePrimeagen
43 Does Your Editor Matter?
Does Your Editor Matter?
ThePrimeagen
44 React Is This Fast???
React Is This Fast???
ThePrimeagen
45 Vim As Your Editor - Vertical Movements
Vim As Your Editor - Vertical Movements
ThePrimeagen
46 Rust for TypeScript devs : Borrow Checker
Rust for TypeScript devs : Borrow Checker
ThePrimeagen
47 Will ChatGPT Terminate Your Job?
Will ChatGPT Terminate Your Job?
ThePrimeagen
48 0 to LSP : Neovim RC From Scratch
0 to LSP : Neovim RC From Scratch
ThePrimeagen
49 From Meth To Netflix
From Meth To Netflix
ThePrimeagen
50 Faster than your favorite JS framework?
Faster than your favorite JS framework?
ThePrimeagen
51 Primeagen Answers the Webs Most Asked Questions for a Software Engineer
Primeagen Answers the Webs Most Asked Questions for a Software Engineer
ThePrimeagen
52 This Algorithm is 1,606,240% FASTER
This Algorithm is 1,606,240% FASTER
ThePrimeagen
53 Looking Under the Hood of JavaScript
Looking Under the Hood of JavaScript
ThePrimeagen
54 What Next...Finding a New Job In Software
What Next...Finding a New Job In Software
ThePrimeagen
55 JSON, I hardly know 'er
JSON, I hardly know 'er
ThePrimeagen
56 RUST Enums ARE Better
RUST Enums ARE Better
ThePrimeagen
57 CoPilot Review: My Thoughts After 6 Months
CoPilot Review: My Thoughts After 6 Months
ThePrimeagen
58 How to Vim in 2023: Tips and Tricks
How to Vim in 2023: Tips and Tricks
ThePrimeagen
59 Twitter's Algorithm Has MASSIVE Problems
Twitter's Algorithm Has MASSIVE Problems
ThePrimeagen
60 Rust Foundation IS DOING WHAT????
Rust Foundation IS DOING WHAT????
ThePrimeagen

ThePrimeagen conducts an experiment to compare the performance of different left pad implementations in JavaScript, highlighting the importance of using native methods and understanding the limitations of micro benchmarks. The experiment reveals that native methods outperform custom solutions, and that micro benchmarks can be misleading due to implementation details and garbage collection. The takeaway is to use native methods whenever possible to achieve the best performance.

Key Takeaways
  1. Create a server to run Apache benchmarking
  2. Set up a benchmarking script to compare different left pad implementations
  3. Ensure proper sleep between each request and manual garbage collection
  4. Send 50 million requests to the server and parse the results
  5. Calculate the average of medians and import results into Google spreadsheets
  6. Analyze the charts to determine the fastest left pad implementation
💡 Native methods outperform custom solutions for left pad implementation, and micro benchmarks can be misleading due to implementation details and garbage collection.

Related Reads

Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →