Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is React's filesize so big given its small API?

Tags:

reactjs

Here are the numbers

  • min+gzip 26k
  • gzip 90k
  • original 450+k

And React doesn't have many features in it's documentation. Why is it so big?

I have a feeling that it's the implementation of lightweight DOM. But I want to be sure.

like image 531
Eldar Djafarov Avatar asked Nov 06 '13 09:11

Eldar Djafarov


People also ask

Why is react file so big?

2 Answers. Show activity on this post. React does quite a bit! The biggest nonobvious part of React is probably the events system -- not only does React implement its own event dispatching and bubbling, it normalizes common events across browsers so that you don't need to worry as much about it.

How big is react Gzipped?

react is 5.3 kb (2.2 kb gzipped), down from 20.7 kb (6.9 kb gzipped).

What is the difference between react and Preact?

Preact and React are both open-source JavaScript libraries that you can use. React is used most for its reusable components, while Preact is preferred for performance reasons. But, we can't say that Preact can replace React. Both have their advantages and disadvantages.


1 Answers

React does quite a bit! The biggest nonobvious part of React is probably the events system -- not only does React implement its own event dispatching and bubbling, it normalizes common events across browsers so that you don't need to worry as much about it. For example, SelectEventPlugin is a built-in event "plugin" which provides an onSelect event which behaves the same way in all browsers.

The virtual DOM implementation does take a decent amount of code as well; a lot of effort is spent on performance optimization, which is why the unminified version includes ReactPerf, which is a tool for measuring rendering performance. In updating the DOM, React also does some convenient things for you like maintaining any input selection and keeping the current scroll position the same.

React also has a few other tricks up its sleeve. One of the coolest is that it fully supports rendering a component to an HTML string even if you don't have a browser environment, so you can send down a page that works even before JS is loaded.


What are you comparing React against? react-15.0.2.min.js is 43k (gzipped), but jQuery is 33k while ember-2.6.0.prod.js is 363k (also gzipped). Obviously these frameworks don't do exactly the same things but they do have a lot of overlap, so I think the comparison is reasonable.

If you're worried about download size, I wouldn't worry too much about JS code contributing to that. Here's an ad which I see on the right side of my Stack Overflow page right now:

Its download size is 95k -- I wouldn't think twice about including an image like that in a page because (even if I was worried about performance) there are usually so many other performance-related things to fix that are more lucrative.


In short, I don't think React is that big, and what size it does have comes from the many small things it does to help you out. You cite React's small API as a reason that React's code should be small, but a better question might be, "How is React's API able to be so simple given all the things it does for you?"

…but that's a totally separate question. :) Hope I answered your question -- happy to expand if I didn't.

like image 66
Sophie Alpert Avatar answered Oct 04 '22 16:10

Sophie Alpert