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 🙏

© 2026 – Pkg Stats / Ryan Hefner

babel-plugin-fix-string-slice-memory

v0.0.2

Published

Fix potential memory leaks in JavaScript engines by forcing string copy on slice/substring/substr.

Readme

babel-plugin-fix-string-slice-memory

npm MIT License

💡 一个 Babel 插件,用于修复 string.slice / substring / substr 的潜在内存泄漏问题,确保截取的是真实的字符串副本。


🧠 背景介绍

在某些 JavaScript 引擎(如 V8)中,调用 slicesubstringsubstr 从大型字符串中截取子串时,返回的子串可能仍然引用原始大字符串的内存。这会导致不必要的内存保留(memory retention)问题。

例如:

const sub = longStr.slice(0, 10); // 可能仍然引用整个 longStr,占用大量内存

而强制触发复制可以避免这种问题:

const sub = ('' + longStr).slice(0, 10); // 强制复制,释放原始字符串内存

✨ 插件功能

将以下代码:

str.slice(0, 10)
str.substring(0, 10)
str.substr(0, 10)

自动转换为更安全的形式:

('' + str).slice(0, 10)
('' + str).substring(0, 10)
('' + str).substr(0, 10)

避免内存泄漏,提升健壮性。


📦 安装方式

pnpm add -D babel-plugin-fix-string-slice-memory
# 或
npm install --save-dev babel-plugin-fix-string-slice-memory
# 或
yarn add -D babel-plugin-fix-string-slice-memory

🔧 使用方式

babel.config.js 中配置:

babel.config.js

const path = require('path');

module.exports = {
  entry: {
    main: './src/index.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index.js',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            plugins: ['babel-plugin-fix-string-slice-memory']
          }
        }
      }
    ]
  }
};

📌 转换示例

输入代码:

const part = longStr.slice(0, 5);
const head = longStr.substring(0, 8);
const tail = longStr.substr(0, 12);

转换结果:

const part = ('' + longStr).slice(0, 5);
const head = ('' + longStr).substring(0, 8);
const tail = ('' + longStr).substr(0, 12);

🧪 运行测试

项目中已包含单元测试:

pnpm test

使用 Vitest 编写快照测试,确保插件行为一致。


🛠 本地开发

本插件属于 monorepo 仓库中的一个子包:

git clone https://github.com/cbtpro/babel-plugins-monorepo.git
cd babel-plugins-monorepo
pnpm install
pnpm build

修改插件代码请前往:

packages/fix-string-slice-memory/