Giter Club home page Giter Club logo

tabling's Introduction

tabling



npm add tabling makes objects lazy

downloads size size

Commit a getter's result for one-time evaluation.

⚙️ Install

npm add tabling

🚀 Usage

import { tabling } from "tabling";

const myObject = tabling({
	get answer() {
		return discover_the_meaning_of_life();
	},
});

myObject.answer; // 42

Where ordinarily, that would look like { answer: discover_the_meaning_of_life() } for a value somebody may never read.

🔎 API

Module: tabling

The main and default module and us Proxy based. Everything is lazy here, the object is built-up over which keys are consumed.

No upfront cost, and slightly slower read time.

Module: tabling/warm

A object based implementation, whereby the result is setup at evaluation time.

Upfront cost, but no read time.

🤔 But which one should I use?

Naturally riddled with assumptions. But If you see the benchmarks below, generally the advice is.

  1. if you've got a long-running process with many reads, the /warm sub-module is what youre after.
    • think like a web-server, where the object is module-scope.
  2. browsers, you're probably after the default (proxy based) api. Very minimal reads, and want fast as possible startup time.

So with that, and by no means bullet-proof answer;

  • server — tabling/warm
  • browser - tabling

💨 Benchmark

via the /bench directory with Node v17.2.0

benchmark :: setup
  default              x 2,442,456 ops/sec ±1.92% (89 runs sampled)
  warm                 x   503,581 ops/sec ±0.69% (93 runs sampled)

benchmark :: jit
  default              x   2,168,685 ops/sec ±1.25% (91 runs sampled)
  warm                 x 430,936 ops/sec ±1.49% (91 runs sampled)

benchmark :: aot
  default              x  27,579,584 ops/sec ±0.52% (89 runs sampled)
  warm                 x 133,821,620 ops/sec ±0.34% (93 runs sampled)

setup — the time to construct the object, without reading from it.

jit — the object is constructed on the hot path, and read from immediately. Think "request bound" objects.

aot — the object is constructed ahead of time, and read later on the hot path. Think "module scope" objects.

❤️ Thanks

Special thanks to @wongmjane for idea!

License

MIT © Marais Rossouw

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.