npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ko-jsx

v0.17.1

Published

An alternative JSX renderer for Knockout.js

Downloads

37

Readme

Knockout JSX

This library is a replacement for Knockout.js' renderer. It trades Knockout's data-bind and DOM traversing for precompiled JSX. Using these techniques allows for dramatic performance improvements across the board putting Knockout in the company of some of the fastest libraries. See JS Frameworks Benchmark.

It accomplishes this with using Babel Plugin JSX DOM Expressions. It compiles JSX to DOM statements and wraps expressions in functions that can be called by the library of choice. In this case ko.computed wrap these expressions ensuring the view stays up to date. Unlike Virtual DOM only the changed nodes are affected and the whole tree is not re-rendered over and over.

To use include 'babel-plugin-jsx-dom-expressions' in your babelrc, webpack babel loader, or rollup babel plugin

"plugins": [["jsx-dom-expressions", {moduleName: 'ko-jsx'}]]

For TS JSX types add to your tsconfig.json:

"jsx": "preserve",
"jsxImportSource": "ko-jsx" 

Installation

> npm install ko-jsx babel-plugin-jsx-dom-expressions

API

There is no ko.applyBinding. Instead the app starts with:

render(AppViewModel, mountEl)

For example:

import { render } from 'ko-jsx'

const Greeter = ({name, onClick}) =>
  <div onClick={onClick}>Hello {name() || 'World'}</div>

function App() {
  const name = ko.observable('John');
  return <>
    <h1>Greeting Example</h1>
    <Greeter name={name} onClick={() => name('Jake')}/>
  </>;
}

render(App, document.getElementById('main'));

Control flow works the way you generally would JSX. However for performant list rendering I have added a fn on subscribable called memoMap that will optimally handle arrays.

const list = ko.observableArray(["Alpha", "Beta", "Gamma"])

<ul>{
  list.memoMap(item => <li>{item}</li>)
}</ul>

Example

Counter

Non-Precompiled

For those who do not wish to use Babel to precompile, the Knockout JSX supports Tagged Template Literals or HyperScript render APIs. These are available when you install the companion library and throw import of 'ko-jsx/html' and 'ko-jsx/h'. Refer to the docs on Lit DOM Expressions, and Hyper DOM Expressions, respectively.

import ko from 'knockout';
import { h, render } from 'ko-jsx/h';

const Greeter = (name, onclick) =>
  h('div', {onclick}, 'Hello', () => name() || 'World');

function App() {
  const name = ko.observable('John');
  return h('div', [
    h('h1', 'Greeting Example'),
    h(Greeter, {name, onclick: () => name('Jake')})
  ]);
}

render(App, document.getElementById('main'));

Compatibility

This does not use any of the Knockout render chain so data-bindings and custom bindings don't work. Knockout Components won't work. Essentially this library only takes the observable change detection part of Knockout. It is compatible with Webcomponents in general. In theory you could call ko.applyBinding and set data-bind attribute value but it seems wasted.

Why?

Knockout.js at it's core is an elegant and efficient solution to tracking change detection. It also as far as modern declarative javascript libraries is one of the oldest. While it's continued to improve over time, in recent years Virtual DOM approaches have gained more popularity. Conceptually it always seemed that Knockout should outperform those techniques but in many areas it's been a dog in benchmarks. After seeing the great Surplus.js it was clear that these sort of libraries still had steam. In the process of working through my own library I realized the approaches used could be generalized to any fine grained library.

Mostly this library is a demonstration of a concept. Fine grained detection libraries shouldn't shy from the technological advancement Virtual DOM libraries brought, when even the oldest of the fine grained libraries still has considerable game in this light.