@_developit on Twitter
@developit on GitHub
Look familiar?
This code was
written in 2008.
... in IE 8, iOS 4 🤡
(artistic liberty)
Unnecessary Terminal
Unnecessary IDE
Unnecessary Email Client
Invention and reinvention
can appear dangerously similar
Maintenance requires large
scope of understanding
When you over-abstract,
you get nothing for free
Idea
Accidentally wrote another template engine
Sad me
<div id="one">
Hello
</div>
<Foo hello />
h('div', { id:'one' },
'Hello'
);
h(Foo, { hello: true })
Remember hyperscript?
It's behind everything now
From blog post "WTF is JSX"
// a "partial" that does a filtered loop
function foo(items) {
return items.map( p => <li>{p}</li> );
}
let vdom = (
<div id="foo">
<p>Look, a simple JSX DOM renderer!</p>
<ul>{ foo(ITEMS) }</ul>
</div>
)
document.body.appendChild( render(vdom) );
Paul Lewis (@aerotwist)
Pot Stirrer
Decent
Surprisingly Decent
Debounced rendering
this was super
annoying to do
Paul Lewis
...again!
It seems to me that developer ergonomics should be less important than our users’ needs.
Something to obsess over.
monomorphic
fn({ a: 1 })
fn({ a: 2 })
megamorphic
fn({ a: 1, b: 2 })
fn({ b: 2, a: 1 })
fn('a', 1)
fn(div)
fn(span)
polymorphic
fn({ a: 1 })
fn({ b: 2 })
fn({ a: 1 })
fn({ a: 1 }, 1)
best
worst
better
fn = o => o.a
Help your friendly engine out a bit:
let value = typeof dom.key!=='undefined' && dom.key
// don't write this:
if (obj.props)
// if you mean this:
if (typeof obj.props!=='undefined')
could be 0, '', null...
Functions can know too much.
function hook(obj, name, a, b) {
return obj[name] && obj[name](a, b);
}
hook(obj, 'foo', 1, 2);
obj.foo(1, 2);
deopt after a few different values
has its own inline cache
The cheapest function call
is the one you never make
- unknown
Monomorphic
Guarded property access
requestAnimationFrame
setTimeout
MutationObserver
Promise.then()