Skip to content

Commit

Permalink
Optimizations for preact/jsx-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Sep 23, 2020
1 parent 3b5182f commit 3311184
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions jsx-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ import { options, Fragment } from 'preact';
/** @typedef {import('preact').VNode} VNode */

/**
* jsx(type, props, key)
* jsxs(type, props, key)
* jsxDEV(type, props, key, __source, __self)
* @fileoverview
* This file exports various methods that implement Babel's "automatic" JSX runtime API:
* - jsx(type, props, key)
* - jsxs(type, props, key)
* - jsxDEV(type, props, key, __source, __self)
*
* The implementation of createVNode here is optimized for performance.
* Benchmarks: https://esbench.com/bench/5f6b54a0b4632100a7dcd2b3
*/

/**
* JSX.Element factory used by Babel's {runtime:"automatic"} JSX transform
* @param {VNode['type']} type
* @param {VNode['props']} props
* @param {VNode['key']} [key]
* @param {string} [__source]
* @param {string} [__self]
* @param {string} [_i]
* @param {*} [_x]
*/
function createVNode(type, props, key, __source, __self, _i, _x) {
// If a Component VNode, check for and apply defaultProps
// Note: type may be undefined in development, must never error here.
if ((_x = type && type.defaultProps)) {
for (_i in _x) if (props[_i] === undefined) props[_i] = _x[_i];
}

function createVNode(type, props, key, __source, __self) {
const vnode = {
type,
props,
Expand All @@ -39,7 +40,15 @@ function createVNode(type, props, key, __source, __self, _i, _x) {
__self
};
vnode._original = vnode;
if (options.vnode != null) options.vnode(vnode);

// If a Component VNode, check for and apply defaultProps.
// Note: `type` is often a String, and can be `undefined` in development.
let defaults, i;
if (typeof type === 'function' && (defaults = type.defaultProps)) {
for (i in defaults) if (props[i] === undefined) props[i] = defaults[i];
}

if (options.vnode) options.vnode(vnode);
return vnode;
}

Expand Down

0 comments on commit 3311184

Please sign in to comment.