{"id":20854421,"url":"https://github.com/w8r/inline-functions","last_synced_at":"2025-05-12T05:31:56.314Z","repository":{"id":179001234,"uuid":"662689025","full_name":"w8r/inline-functions","owner":"w8r","description":"This library allows you to inline functions in your JS/TS code. This is useful for performance reasons, as it allows you to avoid function calls and instead inline the function body directly.","archived":false,"fork":false,"pushed_at":"2025-04-25T15:55:52.000Z","size":82,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-25T16:51:15.703Z","etag":null,"topics":["code-tools","esbuild-plugin","inline-javascript","inline-typescript","optimization","performance","rollup-plugin","vite","vite-plugin"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/w8r.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-05T17:09:14.000Z","updated_at":"2025-04-25T15:55:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"908dfd41-985b-488d-a031-09613efe2473","html_url":"https://github.com/w8r/inline-functions","commit_stats":null,"previous_names":["w8r/inline-functions"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/w8r%2Finline-functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/w8r%2Finline-functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/w8r%2Finline-functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/w8r%2Finline-functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/w8r","download_url":"https://codeload.github.com/w8r/inline-functions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253682408,"owners_count":21946929,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-tools","esbuild-plugin","inline-javascript","inline-typescript","optimization","performance","rollup-plugin","vite","vite-plugin"],"created_at":"2024-11-18T03:25:43.061Z","updated_at":"2025-05-12T05:31:56.304Z","avatar_url":"https://github.com/w8r.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `inline-functions` [![npm version](https://badge.fury.io/js/inline-functions.svg)](https://badge.fury.io/js/inline-functions)\n\nThis library allows you to inline functions in your JS/TS code. This is useful for performance reasons, as it allows you to avoid function calls and instead inline the function body directly.\n\nIt comes as a collection of plugins for different build systems. It is designed to be used with [`vite`](https://vitejs.dev), [`rollup`](https://rollupjs.org), and [`esbuild`](https://esbuild.github.io).\n\nInspired by the clever trick used in [`robust-predicates`](https://github.com/mourner/robust-predicates/blob/c20b0ab9ab4c4f2969f3611908c41ce76aa0e7a7/compile.js) by @mourner.\n\n## Installation\n\n```bash\nnpm install inline-functions [vite] [rollup] [esbuild]\n```\n\n## How it works\n\nThis plugin works by replacing function calls with the function body. This is done by using a macro system. The macro system is a simple object that maps function names to their bodies. For example:\n\n```ts\nimport { Vec2, fn2 } from \"./utils\";\n\n// this is the \"real\" implementation of the function\n// it guarantees that the code will work even if the inlining step is skipped\nconst add = (a: Vec2, b: Vec2): Vec2 =\u003e {\n  a[0] += b[0];\n  a[1] += b[1];\n};\n\nconst foo: Vec2 = [4, 5];\nconst bar: Vec2 = [5, fn2(6, 12)];\n\nconst ZERO = (): Vec2 =\u003e [0, 0];\n\nconst baz: Vec2 = [33, 22];\n// note that you can use the function in the macro,\n// if your replacement function can handle it\nadd(baz, ZERO());\n\n// so that dead code removal doesn't remove the function altogether\nconsole.log(foo, baz);\n\n// this will work, but will be removed as dead code\nadd(ZERO(), bar);\n```\n\nThen the macros to inline the `add()` function calls, would look like this:\n\n```ts\n// counter is used to generate unique variable names\nlet counter = 0;\nconst macros = {\n  add: (a, b) =\u003e {\n    const _a = `_a${counter++}`;\n    const _b = `_b${counter++}`;\n    // this will reference the arguments passed to the function\n    return `\n  const ${_a} = ${a};\n  const ${_b}  = ${b};\n  ${_a}[0] = ${_a}[0] + ${_b}[0];\n  ${_a}[1] = ${_a}[1] + ${_b}[1];\n  `;\n  },\n};\n```\n\nAnd the result of this replacement is:\n\n```js\nvar foo = [4, 5];\nvar ZERO = function () {\n  return [0, 0];\n};\nvar baz = [33, 22];\n// note that you can use the function in the macro,\n// if your replacement function can handle it\nconst _a0 = baz;\nconst _b1 = ZERO();\n_a0[0] = _a0[0] + _b1[0];\n_a0[1] = _a0[1] + _b1[1];\n// so that dead code removal doesn't remove the function altogether\nconsole.log(foo, baz);\n```\n\n## Usage\n\n### Vite\n\n```bash\nnpm install inline-functions vite\n```\n\nThen, in your [`vite.config.ts`](./test/vite.config.ts):\n\n```ts\nimport { defineConfig } from \"vite\";\nimport inlineFunction from \"inline-functions/vite\";\nimport { resolve } from \"path\";\n\nimport { macros } from \"./macros\";\n\nexport default defineConfig({\n  ..., // your config\n  plugins: [inlineFunction({ macros })],\n});\n```\n\n### Esbuild\n\n```bash\nnpm install inline-functions esbuild\n```\n\nThen, in your [`esbuild.config.js`](./test/esbuild.config.js):\n\n```ts\nimport inlineFunction from \"inline-functions/esbuild\";\nimport { macros } from \"./macros\";\nesbuild\n  .build({\n    entryPoints: ...,\n    bundle: true,\n    outfile: ...,\n    plugins: [inlineFunction({ macros })],\n  })\n  .catch(() =\u003e process.exit(1));\n```\n\n### Rollup\n\n```bash\nnpm install inline-functions rollup\n```\n\nThen, in your [`rollup.config.js`](./test/rollup.config.ts):\n\n```js\nimport inlineFunction from \"inline-function/rollup\";\nimport { macros } from \"./macros\";\n\nexport default {\n  input: ...,\n  output: {\n    ...\n  },\n  plugins: [inlineFunction({ macros })],\n};\n\n```\n\n### Verbose mode\n\nFor debugging, you can pass `{ macros: ..., verbose: true }` to the plugin in options, and it will print out all the replacements into `stdout`.\n\n### Limitations\n\nUnpredictable amount, because there's no code analysis, it's just a string replacement. The main limitation for now is that the inlined functions cannot `return`, because it's not obvious how it will fit into the context. You can use the expressions as arguments, but be careful with that. The replacements really have to be simple in order to work efficiently.\n\n### Examples\n\nSee [`test`](./test) for examples and benchmarks.\n\n### Some benchmarks\n\n```\nRunning \"Vite\" suite...\nProgress: 100%\n\n  Inlined:\n    1 464 435 ops/s, ±4.63%   | fastest\n\n  Not inlined:\n    1 187 396 ops/s, ±5.75%   | slowest, 18.92% slower\n\nRunning \"Rollup\" suite...\n\n  Inlined:\n    1 560 921 ops/s, ±1.27%   | fastest\n\n  Not inlined:\n    1 302 231 ops/s, ±0.11%   | slowest, 16.57% slower\n\nRunning \"esbuild\" suite...\n\n  Inlined:\n    1 564 889 ops/s, ±0.80%   | fastest\n\n  Not inlined:\n    1 281 501 ops/s, ±1.02%   | slowest, 18.11% slower\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fw8r%2Finline-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fw8r%2Finline-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fw8r%2Finline-functions/lists"}