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

interpolation-compiler

v1.0.2

Published

Compiles a placeholder-based template to use string interpolation

Downloads

5

Readme

interpolation-compiler - Compiles a placeholder-based template to use string interpolation

| 📖 Documentation | |---------------------------|

Features

  1. Self-Sufficient. The library has zero dependencies
  2. Reliable. The library is written in TypeScript and covered by tests.
  3. Modern. The library comes with native ESM support
  4. Fast. See benchmarks below

Installation

Node.js 20.0.0 or newer is required

  • Using npm (recommended)
    npm i interpolation-compiler
  • Using Yarn
    yarn add interpolation-compiler
  • Using pnpm
    pnpm add interpolation-compiler

Example usage

import { compileSliceBackend } from 'interpolation-compiler';

const HTML_TEMPLATE = `
    <!DOCTYPE html>
    <html html="attrs">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!--head-tags-->
        <!--preload-links-->
    </head>
    <body body="attrs">
        <div id="app"><!--app-html--></div>
        <!--body-tags-->
        <!--teleport-overlay-->
    </body>
    </html>
`;

const render = compileSliceBackend(HTML_TEMPLATE, {
    placeholders: {
        htmlAttrs: 'html="attrs"',
        bodyAttrs: 'body="attrs"',
        headTags: '<!--head-tags-->',
        preloadLinksTags: '<!--preload-links-->',
        bodyTags: '<!--body-tags-->',
        teleportTags: '<!--teleport-overlay-->',
        appHtml: '<!--app-html-->',
    },
});

// Just vague data for clarity :D
const result = render({
    htmlAttrs: '~~htmlAttrs~~',
    bodyAttrs: '~~bodyAttrs~~',
    headTags: '~~headTags~~',
    preloadLinksTags: '~~preloadLinksTags~~',
    bodyTags: '~~bodyTags~~',
    teleportTags: '~~teleportTags~~',
    appHtml: '~~appHtml~~',
});

console.log(result);

// <!DOCTYPE html>
// <html ~~htmlAttrs~~>
// <head>
//     <meta charset="utf-8">
//     <meta name="viewport" content="width=device-width, initial-scale=1">
//     ~~headTags~~
//     ~~preloadLinksTags~~
// </head>
// <body ~~bodyAttrs~~>
//     <div id="app">~~appHtml~~</div>
//     ~~bodyTags~~
//     ~~teleportTags~~
// </body>
// </html>

Why

Why not take mustache?

I need an arbitrary search pattern for the replacement.

Why not just use .replace() or .replaceAll()?

It's slow, well almost... I know the pattern in advance and it will be called many times to replace for a large number of patterns. The big problem is the memory and CPU consumption when dealing with large strings, as the string gets bigger and bigger after replacement.

Why might it be unsafe?

Only template replacement is performed here, no escape html or other filters are applied. And maybe you should avoid compileFnBackend in unknown templates, as it will essentially compile a function, which is comparable to eval.

Benchmarks

  • Machine: WSL 2 Arch Linux 5.15 | AMD Ryzen 9 3900X (24) @ 3.800GHz | Memory: 32GB
  • Node: v21.1.0
  • Run: 2023-11-06 03:01:26
  • Method: npm run benchmark (100ms warmup, 1000ms measure)
┌─────────┬───────────────────────┬─────────────┬────────────────────┬──────────┬─────────┐
│ (index) │       Task Name       │   ops/sec   │ Average Time (ns)  │  Margin  │ Samples │
├─────────┼───────────────────────┼─────────────┼────────────────────┼──────────┼─────────┤
│    0    │  'string.replace()'   │  '302,219'  │ 3308.857712918451  │ '±0.83%' │ 302220  │
│    1    │ 'string.replaceAll()' │  '233,923'  │ 4274.895004354919  │ '±0.54%' │ 233924  │
│    2    │   'replace backend'   │  '228,054'  │ 4384.916371057442  │ '±0.57%' │ 228055  │
│    3    │    'slice backend'    │ '4,342,870' │ 230.26243492081838 │ '±0.73%' │ 4342871 │
│    4    │     'fn backend'      │ '8,637,111' │ 115.7794436350917  │ '±0.92%' │ 8637112 │
└─────────┴───────────────────────┴─────────────┴────────────────────┴──────────┴─────────┘