Compile JSX right into virtual node objects avoiding `h` function
See original GitHub issueThere are an idea to avoid using h
in favor of compilation step which will do the same work.
Compile JSX:
const view = (state, actions) => (
<main>
<h1>{state.count}</h1>
<button onclick={actions.down}>-</button>
<button onclick={actions.up}>+</button>
</main>
)
to:
const view = (state, actions) => ({
name: 'main',
props: {},
children: [
{ name: 'h1', props: {}, children: [state.count] },
{ name: 'button', props: { onclick: actions.down }, children: ['-'] },
{ name: 'button', props: { onclick: actions.up }, children: ['+'] }
]
})
Potentially the app should work faster this way because each function call requires additional computing resources. Also h
which is still necessary for JSX haters could be tree shakied from your build.
Possible future improvements: adopt babel-plugin-transform-react-constant-elements to speedup the app even more:
const h1 = { name: 'h1', props: {}, children: ['Static content'] }
const view = (state, actions) => h1
Can anyone foresee potential pitfalls?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:27 (21 by maintainers)
Top Results From Across the Web
Render Functions & JSX | Vue.js
h () is short for hyperscript - which means "JavaScript that produces HTML (hypertext markup language)". This name is inherited from conventions shared...
Read more >Introducing JSX - React
By default, React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that's...
Read more >Can I use jsx without React to inline HTML in script?
I was able to write JSX files and inject them into an HTML page using a 'fake' React file. no-react.js /** * Include...
Read more >JSX Alternatives - Medium
JSX is a very popular choice nowadays for templating in various frameworks, not just in React. However, what if you don't like it, ......
Read more >Class: VComponent - Oracle
Virtual DOM nodes are plain old JavaScript objects that specify the node type ... Virtual component render functions support the use of JSX...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@jorgebucaran Here’s a taste of the JS Framework Bechmark results for lazy/thunks, run in-browser and scientifically clicking around at a steady pace. I’ll run the full automated test again soon. I ran it last night, but my implementation was wrong, so the results were way too good to be true. Now that I’ve fixed it, the results line up with my expectations, showing big wins on partial page updates. I removed percentages which were within 20% as not being significant.
I know it’s been said before that generating the tree isn’t that expensive, but it can really add up on a large app, or even a small one that has complex logic to generate the VNode tree. It’s a lot faster to do a few strict
===
checks around some components, and generate the new node only if needed.I’m look forward to combining these lazy functions with the custom JSX optimizations to get the best of both worlds: faster node creation, and faster partial updates 😎 My computer is going to hate me after running all of these variations through JS Framework Benchmark 🖥️ 💥
The API is pretty ugly right now, so I’ll PR once I’ve thought of a nicer way to use this and learn how to write tests for it.
So… I’ve recently decided to not hate on JSX so hard 😆
The readability is so much higher than nested
h
calls in large view functions, that I really want to convert my main project. However, the runtime perf costs due to the variable length arguments and extra function calls (h(Component)
) are hard to swallow.Today I’ve been working on getting babel-plugin-transform-jsx to work with
hyperapp
.I’m new to Babel transforms, but I managed to get this working! The transform code was quick & dirty, but I’m really pleased with the results so far 😄
As a proof of concept, here is my recent Lazy Components Codepen demo run through
babel-plugin-transform-jsx
then my own custom transform: https://codepen.io/SkaterDad/pen/wmBKbM?editors=0010Quick rundown:
children: null
tochildren: []
when needed.h(Component, props, children)
, justComponent(props, children)
.Next steps:
babel-plugin-transform-jsx
as a dependency, or maybe fork it?@hyperapp
NPM scope?~babel-plugin-transform-hyperapp-jsx
or something to fit in with the rest of babel plugins?h
calls. Should be higher since there are less functions being invoked at runtime.