Skip to content
Swizec Teller - a geek with a hatswizec.com

Just for fun ๐Ÿ‘‰ React vs. jQuery vs. Svelte, same ๐Ÿฑ app

You know that feeling when you meet an old friend you haven't seen in years? You used to be so close but ...

"Long time huh ..."

"Yeah time flies ... how's the wife?"

"Girlfriend. She's good, just started a new job ..."

"Nice nice ... you still at that startup?"

"Yeah ... it's okay ... you?"

"Nah, new gig last year. Think I told you, dunno"

"Maybe you did, hard to keep track ... liking it?"

A beer or two later and you're back to being best buds. Suddenly it's 1am and a long yawn reminds you that you're not quite so young anymore.

Click through for source
Click through for source

That's what messing around with jQuery felt like after all these years.

I was bored Monday night and decided to see if I still know how to build with jQuery. Nothing better to do, no pressing ideas, a bit of fun.

Also a favor for a friend who's preparing to talk about React to a bunch of people who've never seen it before ๐Ÿ˜›

It's funny, jQuery is so old now (2 years since new versions stopped coming) I feel like I almost have to introduce the darn thing. Used to be the backbone of the web, now it's in the dustbin of history.

Pushed web development forward for its entire 10 year run. Oh the memories ...

memory_lane giphy

So, how does jQuery stack up to React and how does React stack up to Svelte? Let's see ๐Ÿ‘‡

React app

Click through for source
Click through for source

Started with a React app. It's what I'm most familiar with right now.

We built a cat app. Fetches 5 cats from thecatapi, shows first cat. Click a button, get more cats.

When app runs out of cats, it fetches more.

Click through for source
Click through for source

fetchCats returns a promise with a bunch of cats. Pagination works based on current count of cats we've got.

Rendering a list of cats happens in a <Kitties /> component. 'tis but a loop

Click through for source
Click through for source

Take list of cats, count how many the user wants to see, slice the array, render each cat as an image. ๐Ÿ‘Œ

Driving this logic with React Hooks isn't bad. React's ergonomics are getting real good.

Click through for source
Click through for source

We got useState for the count of cats, and a useEffect to deal with fetching. Fetches on first load and makes sure to fetch more when showCount is bigger than the current amount of cats.

React handles the rest.

magic giphy

jQuery app

Click through for source
Click through for source

The hardest part of building the jQuery version was figuring out how to import jQuery in a modern compiled-JavaScript app. ๐Ÿ˜‚

No seriously, I struggled, tried a bunch of things, and ended up with a good old script tag and a public CDN.

Click through for source
Click through for source

People don't do that in real life anymore. It's all import this import that.

For the app itself we used a naive unoptimized approach much like many jQuery apps of old ๐Ÿ‘‰ find div, empty div, render everything from scratch every time.

Click through for source
Click through for source

Notice the mess with creating a small component tree of <div><img></div>? Painful. JSX improved this a lot. Almost can't believe we used to build complex apps like this.

You can see it's slow too.

Open the CodeSandbox, add a couple cats. You'll see the more cats there are the slower the app gets.

Not so with React because React avoids re-rendering components that don't change.

PS: check out this mess of attaching a button click handler and driving the cat fetching logic.

Click through for source
Click through for source

๐Ÿคฎ

I like hooks.

Svelte app

Click through for source
Click through for source

The Svelte version wins in the immense straightforward simplicity of it all. I'm pretty blown away.

Check out the <Kitty /> component

Click through for source
Click through for source

Gets an id and a url, has some style, and renders a div with an image. Pretty slick, looks almost like pure HTML.

Then you got the component that drives state and renders a bunch of kitties with a button.

Click through for source
Click through for source

The script looks similar to React Hooks. You got some variables with state and you got a moreCats method that drives it.

Repeating that array merging logic twice is a little less elegant than the useEffect hook, but probably easier to understand how it works ๐Ÿค”

Rendering is neat too

Click through for source
Click through for source

Similar to React, but we used {#each} instead of JavaScript's .map. Still not sure how I feel about that but it's pretty readable you gotta admit.

๐Ÿค”

Between you and me I think React wins this one. Maybe it's because I'm used to it.

But Svelte does look like it might be quicker to grasp for someone who's new to modern JavaScript.

What do you think?

Happy Friday,
~Swizec

Did you enjoy this article?

Published on May 24th, 2019 in Front End, Livecoding, Technical

Learned something new?
Want to become a high value JavaScript expert?

Here's how it works ๐Ÿ‘‡

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet ๐Ÿ“–right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet ๐Ÿ“–

Then get thoughtful letters ๐Ÿ’Œ on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. ๐Ÿ‘Œ"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. โœŒ๏ธ

Have a burning question that you think I can answer?ย I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own? ย Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first ๐Ÿ’ฐ on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax?ย Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you areย โค๏ธ

Created bySwizecwith โค๏ธswizec.com