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

babel-plugin-transform-lodash-get

v1.0.7

Published

Babel plugin to statically optimize lodash/get

Downloads

26

Readme

babel-plugin-transform-lodash-get

Babel plugin to statically optimize lodash/get

Motivation

lodash/get is a quick and dirty way to follow a path in an object and avoid an Uncaught TypeError: Cannot read property of undefined, but it's quite inefficient because it does a lot of things at runtime which can usually be done at compile-time. This is especially true with the string version, which involves a regular expression evaluation to determine the path to take. Here are some benchmark results (see misc/bench.js) that demonstrate what this plugin accomplishes:

get array x 6,064,571 ops/sec ±0.64% (93 runs sampled)
get string x 1,757,073 ops/sec ±0.69% (91 runs sampled)
transformed strict x 112,676,455 ops/sec ±0.75% (92 runs sampled)
transformed loose x 213,763,702 ops/sec ±1.16% (89 runs sampled)
native no check (control) x 884,326,945 ops/sec ±0.39% (92 runs sampled)
native (caught) x 224,134 ops/sec ±0.84% (77 runs sampled)

strict
------
1757.95% faster than get array
6312.74% faster than get string
87.26% slower than native no check (control)
50171.86% faster than native (caught)

loose
------
3424.80% faster than get array
12065.90% faster than get string
75.83% slower than native no check (control)
95273.07% faster than native (caught)

You can run this benchmark on your own setup as follows:

npm run bench

Example

In

get(obj, 'products.foo.items[0].baz.items[0].foobar', 'gg')

Out

let _gg;
let _obj;
(_gg = 'gg', obj && (_obj = obj.products) && (_obj = _obj.foo) && (_obj = _obj.items) && (_obj = _obj[0]) && (_obj = _obj.baz) && (_obj = _obj.items) && (_obj = _obj[0])) ? (_obj = _obj.foobar) === void 0 ? _gg : _obj : _gg;

loose mode

The strict transformation (the default) generates code that always evaluates to exactly what lodash/get would produce, but if you favour better performance and smaller transformed code at the expense of sometimes getting a different falsy value than undefined when the key is not found in the object, then you can use the loose mode, that produces the following for the above example:

Out

let _obj;
obj && (_obj = obj.products) && (_obj = _obj.foo) && (_obj = _obj.items) && (_obj = _obj[0]) && (_obj = _obj.baz) && (_obj = _obj.items) && (_obj = _obj[0]) && _obj.foobar || 'gg';

What it doesn't do

If the second argument of to a lodash/get call is not a string or an array but something else (like a variable, function call, object lookup), it cannot be statically optimized, obviously because this plugin cannot predict what would end up there at runtime. In these cases, the plugin would leave it as it is and move on to the next.

Also, this plugin doesn't (yet) remove unused imports/requires of lodash after transform from files; you may have to use other babel plugins to deal with this.

Options

Apart from the aforementioned loose mode, this plugin also supports an additonal patterns option, which defaults to ["get", "_.get"], which lets you add custom patterns to match, but this is not recommended.

Installation

Via pnpm

pnpm i -D babel-plugin-transform-lodash-get

Via npm

npm i -D babel-plugin-transform-lodash-get

Via yarn

yarn add -D babel-plugin-transform-lodash-get

Usage

Via .babelrc (Recommended)

.babelrc Without options

{
  "plugins": ["transform-lodash-get"]
}

With options

{
  "plugins": [["transform-lodash-get", { "loose": true, "patterns": ["foo", "_.foo"] }]]
}

Via CLI

babel --plugins transform-lodash-get script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["transform-lodash-get"]
});