Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
babel-plugin-optional-chaining-retro
Advanced tools
This is a retrospetive trasformation plugin of EcmaScript optional chaining syntax.
Assume we have this object with some deeply nested keys:
const obj = {
alpha: {
beta: {
charlie: {
delta: "hello",
},
},
},
};
And we have some code snippets getting optional values deep inside
// 108 Bytes (comments excluded)
// 1x size, take it as a baseline
const x = obj?.alpha?.beta?.charlie;
// x = {delta:"hello"}
const y = obj?.alpha?.beta.charlie?.delta;
// y = "hello"
const z = obj?.alpha?.fox
// z = undefined
(Try it yourself on the Babel Repl)
// 790 Bytes, 7.31x inflation
// Code length complexity ~ O(7N)
"use strict";
var _obj, _obj$alpha, _obj$alpha$beta, _obj2, _obj2$alpha, _obj2$alpha$beta$char, _obj3, _obj3$alpha;
const x = (_obj = obj) === null || _obj === void 0 ? void 0 : (_obj$alpha = _obj.alpha) === null || _obj$alpha === void 0 ? void 0 : (_obj$alpha$beta = _obj$alpha.beta) === null || _obj$alpha$beta === void 0 ? void 0 : _obj$alpha$beta.charlie;
const y = (_obj2 = obj) === null || _obj2 === void 0 ? void 0 : (_obj2$alpha = _obj2.alpha) === null || _obj2$alpha === void 0 ? void 0 : (_obj2$alpha$beta$char = _obj2$alpha.beta.charlie) === null || _obj2$alpha$beta$char === void 0 ? void 0 : _obj2$alpha$beta$char.delta;
const z = (_obj3 = obj) === null || _obj3 === void 0 ? void 0 : (_obj3$alpha = _obj3.alpha) === null || _obj3$alpha === void 0 ? void 0 : _obj3$alpha.fox;
// runtime keyPath helper
// a constant size dependency
import get from "lodash.get"
// 165 Bytes below, 1.53x inflation
// Code length complexity ~ O(C+1.5N)
"use strict";
const x = get(obj, ["alpha", "beta", "charlie"]);
const y = get(get(obj, ["alpha", "beta"]).charlie, "charlie");
const z = get(obj, ["alpha", "fox"]);
We can see the transpiled code using a runtime keyPath
function costs 1.53x
inflation vs the 7.31x
overhead in the current babel transpiler.
That is ~4.78x
save of space in our naive example. Of course, the optional chaining won't normally happen that much in your code.
I'm glad you asked.
keyPath
function, depending on its implementation. Here we provide 2 options:
lodash.get
: The most commonly used keyPath
helper, costs 4.4KB minified, or 1.8KB minified and gzippeddlv
: A geniously minimalist implementation from the author of PreactJS, costs 253B minified, or 191B minified and gzipped.dlv
helper sets me back by ~30% on my laptop vs the vanilla babel output.FAQs
A retro implementation of optional chaining
The npm package babel-plugin-optional-chaining-retro receives a total of 2 weekly downloads. As such, babel-plugin-optional-chaining-retro popularity was classified as not popular.
We found that babel-plugin-optional-chaining-retro demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.