Code Monkey home page Code Monkey logo

nodejs-closure-serializer's Issues

Support transforming AST prior to serialization

It should be possible to transform the AST syntax prior to serialization rather than relying purely on value-level serialization customization.

This would be useful for removing syntax added by Functionless's TS transformers.

Add a README

Add a README and be sure to mention that this is a fork.

`with` degrades performance significantly

The way the closure serializer wraps closures in a with degrades performance by approximately 4-5x. See below for an example.

function foo() {
  with({
    a: "hello"
  }) {
    return (function() {
      return function() {
        return arguments * 10 ;
      }
    }).apply(undefined, undefined).apply(this, arguments);
  }
}


function foo2() {
  return (function() {
    return function() {
      return arguments * 10;
    }
  }).apply(undefined, undefined).apply(this, arguments)
}

for (let j = 0; j < 10; j++) {
  const start = new Date();
  for (let i = 0; i < 10*1000*1000; i++) {
    foo2(i);
  }
  
  console.log(new Date().getTime() - start.getTime());
}

Maintain references to closured `let` and `var`

Closure serialization should maintain references to closured let and var and all modifications of that value should reflect everywhere it is referenced.

Below is a test case that should pass, with expectResult: 2, but the result is 0 - because the closured let is re-declared in each closure and copied by-value, not by-ref. This is true even when using the previous with technique.

let i = 0;
function a() {
  i += 1;
}
function b() {
  i += 1;
}

cases.push({
  title: "two functions modifying a single let have changes reflected",
  func: () => {
    a();
    b();
    return i;
  },
  snapshot: true,
  expectResult: 2,
});

Update the serialize contract

The repo is a fork of @pulumi/pulumi's closure serializer.

One of the changes to the original repo was to support replacement of objects to be serialized using the serialize callback.

The current implementation supports the contract (o: any) => boolean | any where:

  • true - serialize o as is - original contract
  • false - do not serialize o (replace with undefined) - original contract
  • anything else - replace o with the value

This contract isn't great, but it was a quick update from the original contract.

We should update this to support the edge case where a boolean value should replace the original value.

Proposal:

/**
 * return the value to serialize, return `undefined` to skip serialization. `undefined` may also be used to avoid circular references.
 **/
serialize: (o: any) => any | undefined

In the updated contract, the user just returns the new value or current value.

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.