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