Is JSON Blazingly Fast or...?
Key Takeaways
ThePrimeagen explores the performance of JSON, a popular data interchange format, and discusses its potential drawbacks.
Full Transcript
Json JavaScript objects notation properly pronounced and was almost called the Gizmo Gizmo dismal am I like four years old it's funny it's the canonical information interchange that we use on the internet today it's used in Twitter it's used on Facebook it's used on YouTube it's used on Netflix it's used on Twitch it's used everywhere see if I were to subscribe to say the primagen and I press subscribe you'll see right here that we did send off and boom Jason is used as the response to update the UI why don't you give it a try see if your network tab shows that is just on when you subscribe to me but here's the thing about Gizmo sorry Jason is that it is slow it is incredibly slow it is outlandishly slow and yes no I get it XML is worse but not by that much so today I am gonna show you how Jason works and even more importantly why it's slow so what is Json well imagine this guy right here wants to communicate to Twitch full via the cloud the thing about the cloud is that it can only take in zeros or ones you know that's why you have this rating like mega bits or gigabits per second that effectively means how many zeros and ones it can send per second a million zeros and ones per second would be one megabit per second so if we look at this object right here we can't just send this across the wire right the wire does not understand an object it understands zeros and ones that is why we would take something like this and turn it into a string because a string is really just a series of bytes and bytes are eight bits we can send those across the wire of course when you Json stringify him it actually looks a lot like that JavaScript above that is why it's called JavaScript object notation because it literally is Javascript we can actually just simply jump up here and go const b equals this and it's just valid JavaScript but the thing that is kind of interesting about JavaScript object notation are Json or Gizmo it was almost Gizmo is that every time it has to specify the key and the value it is in the most verbose way possible because it the schema is contained within the data that means to represent this single byte object right here this kind of this type discriminator you actually need to have about nine bytes to represent that one byte which of course is four for the word type two for the quotes one for the colon and of course one for the comma time stamps are equally terrible a timestamp can be represented with eight bytes but this right here actually is represented with 27 bytes because every single number takes a byte so this right here these last ones right here is how much it should take overall but instead you have all that extra fluff on the end it's also not that great when it comes to complex types like this one you have extra two bytes for you know the brackets on the end and then each number has to be represented by its length in bytes so let's just say these were each one byte you could tightly pack this as two bytes and it would be a two byte array but instead it's going to take eight bytes to represent the exact same piece of information not to mention the key and the quotes and the colon on this side and if there was more data there would be an extra comma I know you're sitting there thinking ads just bytes this can't be that big of a deal I'm in megabytes I'm in gigabytes there's a lot more that happens with Json than just simply how many bytes are transferred across the wire remember when you Json parse something it needs to take this create the key create the value put that in a newly constructed object go to the next one parse out the key parse out the value put that into an object or create intermediate objects if needed it has to do this one at a time as it goes through your Json type and if you're ever curious about how this works you here you go on json.org you can actually see all the flow diagrams that do happen to be able to parse out every single value and as you can see it's a it's quite a bit of work right here it has to read it one byte at a time also if you look at this thing right here this is the reason right here why a trailing comma breaks your package Json every time if they would have just put an extra one right here we would have been so good but no no no no no no Json does not like those trailer commas all right so let's talk about an alternative encoding so I just moved this over to rust so you can kind of see this right here this is called an enum this is technically a some type I have a rating which has an inner object or the type inside of it is a eight byte for a timestamp plus one byte for the data so you could imagine that I would need one byte for the type of enum that I'm going to have plus I'm going to need eight bytes to represent the timestamp plus I'm going to need another one byte to represent that piece of data right here and of course the coordinate is going to be the exact same thing except for I'm going to need two by lights at the end to represent these two bytes in an array and so to be able to represent the exact same information I only need about 10 or 11 bytes whereas on the other side it's around 50 bytes to represent the same thing so about a 5x saving in size so that's kind of like the overall reason why Json is used and of course why it is not that great right but there's actually more to it and so I want to show you a little experiment I made just to really test how fast or how slow Json is and of course you know what that means if we're doing experiments and we're going blazingly fast we need to do something we need to change this attire oh it's science time foreign [Music] all right so this is the simple test so first this is the server this is a very important part I'm just creating a raw TCP server in which people can make TCP connections to with that the message is going to look a little something like this the first byte of the message will be the length of what is going to be in the message the next will be the stringified say representation of the object using Json or the Deku encoding of course Deku encoding is named after Zelda Ocarina of Time and it is effectively just that simple binary representation that we talked about I have to do a bit of annotation blah blah blah and on the rest side I can create these really small objects instead of the Json encoding so that means in JavaScript either write a Deku deserializer so you can see right here I actually only read out one byte because the data in my messages is effectively a type and byte I use the type as the function to call right here so that who am I function is called right here if the first byte is a who am I bite and of course I have to be able to read all this data out of the stream of data coming in from the TCP socket so I just simply get the parse call and then after that I actually parse out the message and then send it on to the program if it's just just on I do the exact same thing I read out the length of this message right here and then I simply do a simple sub array into this buffer and then to string it and then parse it and as you can see here I don't do anything with the object I just simply let go of it right away I don't even try to hold on to these newly parsed items I'm just saying how fast can the server handle parse and of course I took the time to write a client in Rust I spawn about a hundred threads sending things as fast as possible and I send them in chunks of a thousand I think at a time and I either serialize them in Json or I serialize them using Deku and of course I only do the serialization once of course at the top of the program the rest of the time I'm just sending copies of that data over and over again so maximum blazingly fast all right so let's just try the experiment out so I have the Json server running right here the server is bound it's ready to rock I'm gonna jump over and do I the the rust client saying hey encode your things via Jason and I'm going to send it as fast as possible and the server is going to run for 10 seconds of received messages and then stop and tell me how many objects per millisecond can be created all right give it a second I did say 10 seconds you know it has to run for a little bit and there you go it ran for 10 seconds it created a lot of objects and so that's about 1665 objects per millisecond Json can create now remember we don't hold on to anything we don't do anything special we just let it run all right so let's run the Deku server let's jump over here and run the Deku client now remember the Json server did about 1665 objects per millisecond where's the Deku one did about 24 000 objects per second so over 10x Improvement that is significantly faster so what I don't want you to do is just go out there and just convert right away to Binary do some research see how much requests are going on make sure you kind of you know profile your server do an experiment see what happens to your server because remember Json is easy Jason is a very simple you know algorithm it's very simple to see the output it's very simple to inspect where's these binary encodings can be quite difficult to work with and version ending is not that easy just like versioning is just on it also is not easy your client just gets data that it can't understand that's the only difference is that the schema is within the Json whereas you're assumed to know the schema when it comes to these binary protocols it's not a protocol it's really an encoding if you think about it's a serialization and deserialization and so it just on slow slow absolutely it's just horrifyingly slow would the world be a better place if we never use Json probably I mean we'd probably be using half the servers that we're using today on Earth if we just got rid of all that crappy is just on encoding again this is the second kind of Deep dive more intense video on something if you like this let me know down below show me those algorithmic signals of course subscribe to the channel blah blah blah blah you get the idea because if you don't do it I don't know that you like this I think that you hate it and I won't do it even though I like it and of course everything that we developed today even the results and everything we're all done live on Twitch so if you want to come check it out come check it out the name is the scienceogen Jizzle I can't believe I can't believe it was almost called Jizzle like how much better would life have been if it was called Jizzle
Original Description
Is Json really that slow? Can the most popular interchange format really be that bad?
### 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 He is also open to do more editing, so slide deeeeeeeeep into his dms.
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
#vim #programming #softwareengineering
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from ThePrimeagen · ThePrimeagen · 38 of 60
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
▶
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 AI Lessons
⚡
⚡
⚡
⚡
When AI Writes Most of My Code: What Happens to My Identity as a Software Engineer?
Medium · AI
When AI Writes Most of My Code: What Happens to My Identity as a Software Engineer?
Medium · Programming
How AI Is Changing Software Development (2023–2026)
Medium · Machine Learning
How AI Is Changing Software Development (2023–2026)
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI