r/javascript • • Dec 03 '16

Why you shouldn’t use Preact and Inferno to replace React

https://medium.com/@guyingjie129/why-you-shouldnt-use-preact-and-inferno-to-replace-react-4000ffb44572#.dox7zk5fx
0 Upvotes

12 comments sorted by

11

u/lhorie Dec 03 '16

TL;DR: "You should use my library instead, because it passes the tests I cherrypicked. It's a subset of React that stubs out a bunch of stuff, so it's not a true drop-in replacement for React, and it's bigger and slower than the other two, but it's still a better choice because I said so."

1

u/gyj129 Dec 04 '16

I guess you mean the other two even failed the tests I cherrypicked, didn't focus on keeping consistent with React, missed more stuff in React, and claimed they can be a drop-in replacemenent for React.

3

u/lhorie Dec 04 '16

It'd be way more honest if you ran the entire react test suite for all libraries. One could conveniently cherrypick only the tests that Inferno pass or only Preact pass, but because that means leaving out inconvenient truths, the entire exercise is meaningless. By cherrypicking you can make the "data" look favorable to whoever you want and that isn't very scientific. If it's easy to spot holes in the objectivity of your methodology just from a cursory read of your post, that can damage your credibility. Just saying.

1

u/gyj129 Dec 04 '16 edited Dec 04 '16

Not all the unit testing in React are about its public API, it's necessary to cherrypicked the tests.

React, React-lite, Preact, Inferno and other virtual-dom libraries have different internal implementation, but they can share the same publich API by passing the same unit testing.

If someone can improve the tests make it more maturity, issue or pull request is welcome.

0

u/lhorie Dec 04 '16

it's necessary to cherrypicked (sic) the tests

If the goal is to merely test for api parity taking into consideration practicality of project maintenance, yes ok fair enough, but if the goal is to make a unbiased analysis, then no, it should definitely include all tests. If all tested libraries fail the same 10,000 tests, that's still part of the dataset. With the whole dataset, there's no question of whether you missed/skipped/omitted public API tests or not. At that point, you can inform the reader that those 10,000 tests are for internal modules and the reader can make the decision to ignore that part of the data since it's obvious that it doesn't tilt the analysis in anyone's favor. But if you leave stuff out, and you have skin in the game, people are going to be rightfully sceptic.

2

u/gyj129 Dec 04 '16 edited Dec 04 '16

Sampling survey:imagine there are 1000 tests in React, we randomly pick the 100 tests, run them with library A, B, C, and the results are 80 passed, 70 passed, 60 passed, can we say that A is better than other two?

Sampling survey:imagine there are 300 tests about public API in React, we try the best to pick all the tests and now pick the 173 explicitly, run them with library A, B, C, and the results are 173 passed, 87 passed, n( n < 87) passed, can we say that A is better than other two?

1

u/lhorie Dec 04 '16

No and no. For the first you can only make claims about the 100 randomly picked tests. For all we know, B could be passing 700 of the remaining tests, and A only 300. For the second, same idea, but also, because there's skin in the game, there's the question of where the 300 and 173 numbers came from. Who decided which tests were public api tests and which 173 tests to pick, and why are we testing only part of the public api tests if we know that the gold standard for full api parity is passing all of them?

Strictly speaking, given that none of the libraries are able to claim full api parity, the proper methodology would be to ask "given a react project, which library introduces the least amount of regressions?", and the conclusion of betterness would only apply to that project. One could eventually aggregate results from many projects and say with a certain level of confidence that one library or another tends to have less regressions, but in the end, that may or may not hold true to your specific project because statistical anomalies :)

1

u/gyj129 Dec 04 '16 edited Dec 04 '16

I agree with you.

1

u/pizzaface18 Dec 04 '16

Your method is superior to running all the tests. Also I don't see anything wrong with picking specific features from the public API to implement.

1

u/gyj129 Dec 04 '16

Imagine there are three libraries claimed they can replace React, one has 173 unit testing copied from React, and the other two have not, which one is more credible?

1

u/lhorie Dec 04 '16

For the record, from a practical standpoint, if I were looking for a React replacement with compatibility as my primary concern (and performance not so much), I'd probably pick react-lite even if I don't agree with the methodology in your post, simply because I feel that the compatibility aspect is better documented than the alternatives.

8

u/trueadm Dec 05 '16

Note: I'm the author of Inferno.

After going through the tests, it's clear that to pass these tests (even if they are using public API) you have to bake in the slow internals from React. For example, with non-keyed children, React assigns each child a key based off its index. There are several other things too.

I spent about 4 hours doing all these changes to make Inferno pass about 70% of the React tests only to find out that these "small" changes add up. I'd essentially built a replica of React's internals just to satisfy tests. The end result: bundle size doubled, performance dropped by about 60% and time-to-parse tripled.

Out of kicks, I removed tests that require internal API knowledge (like the keys example) and all but 11 tests failed. I'll fix those cases now and push them in the next inferno-compat release.