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 🙏

© 2025 – Pkg Stats / Ryan Hefner

paek

v1.0.3

Published

High-performance PAEK line smoothing algorithm with O(n) time complexity

Downloads

21

Readme

paek

高性能PAEK(Partially Averaged Edge Kernel)线平滑算法库,支持 ESM/CJS/IIFE 多格式,适用于GIS轨迹、矢量图形等场景,通过SIMD加速、内存复用实现O(n)时间复杂度,兼顾速度与精度。

特性

  • 极致性能:O(n)线性复杂度,100万点数据处理耗时<200ms,内存占用降低75%+
  • 多格式支持:原生支持ESM(现代前端)、CJS(Node.js)、IIFE(无构建浏览器环境)
  • 智能优化
    • SIMD加速(支持时自动启用,老旧环境降级)
    • 内存复用(减少50%临时内存分配)
    • 动态核因子(根据容差自适应调整,平衡精度与效率)
  • 高兼容性:Node.js 14+、现代浏览器(Chrome/Firefox/Safari 14+)

安装

1. npm/yarn/pnpm(推荐)

# npm
npm install paek --save

# yarn
yarn add paek

# pnpm
pnpm add paek

2. 浏览器直接引入(IIFE)

无需构建工具,直接通过CDN引入:

<!--  unpkg CDN -->
<script src="https://unpkg.com/paek@latest/dist/index.global.js"></script>

<!--  jsDelivr CDN -->
<script src="https://cdn.jsdelivr.net/npm/paek@latest/dist/index.global.js"></script>

快速开始

场景1:Node.js/CJS

// 引入CJS模块
const { paek } = require('paek');

// 1. 生成测试数据(Float32Array:[x0,y0, x1,y1, ...])
const rawData = new Float32Array([
  0, 0, 1, 3, 2, 1, 3, 4, 4, 2, 5, 5, 6, 3
]);

// 2. 平滑处理(仅需指定容差)
const smoothedData = paek(rawData, { tolerance: 0.8 });

// 3. 输出结果
console.log('原始数据:', rawData);
console.log('平滑后数据:', smoothedData);
console.log('平滑后点数量:', smoothedData.length / 2);

场景2:现代前端/ESM(Vite/Webpack 5)

// 引入ESM模块(支持Tree Shaking)
import { paek, type PAEKConfig } from 'paek';

// 1. 从API获取GPS轨迹数据(假设为Float32Array)
const fetchTrajectory = async (): Promise<Float32Array> => {
  const res = await fetch('/api/trajectory');
  const data = await res.arrayBuffer();
  return new Float32Array(data);
};

// 2. 平滑处理
const processTrajectory = async () => {
  const rawData = await fetchTrajectory();
  const config: PAEKConfig = {
    tolerance: 1.2, // 控制平滑程度(值越大越平滑)
    reuseBuffer: true, // 复用输入缓冲区,减少内存占用
    precision: 1e-6 // 浮点计算精度
  };
  const smoothedData = paek(rawData, config);
  
  // 3. 渲染到地图(如Leaflet/Mapbox)
  renderToMap(smoothedData);
};

processTrajectory();

场景3:浏览器/IIFE(无构建工具)

<script src="https://unpkg.com/paek@latest/dist/index.global.js"></script>
<script>
  / 1. 生成随机折线数据
  const generateData = (pointCount) => {
    const data = new Float32Array(pointCount * 2);
    let x = 0, y = 0;
    for (let i = 0; i < pointCount; i++) {
      x += (Math.random() - 0.5) * 2;
      y += (Math.random() - 0.5) * 2;
      data[i * 2] = x;
      data[i * 2 + 1] = y;
    }
    return data;
  };

  // 2. 平滑处理
  const rawData = generateData(1000);
  const smoothedData = paek.paek(rawData, { tolerance: 0.5 });

  // 3. 绘制到Canvas
  const canvas = document.getElementById('map-canvas');
  const ctx = canvas.getContext('2d');
  ctx.beginPath();
  ctx.moveTo(smoothedData[0], smoothedData[1]);
  for (let i = 2; i < smoothedData.length; i += 2) {
    ctx.lineTo(smoothedData[i], smoothedData[i + 1]);
  }
  ctx.strokeStyle = '#2196F3';
  ctx.lineWidth = 2;
  ctx.stroke();
</script>
<canvas id="map-canvas" width="800" height="400"></canvas>

API 文档

核心函数:paek(data: Float32Array, config: PAEKConfig): Float32Array

参数说明

| 参数 | 类型 | 描述 | |--------|---------------|----------------------------------------------------------------------| | data | Float32Array | 输入线数据,格式为 [x0, y0, x1, y1, ..., xn, yn],必须包含≥2个点 | | config | PAEKConfig | 平滑配置,必填项为 tolerance,其他为可选 |

配置接口:PAEKConfig

| 配置项 | 类型 | 默认值 | 描述 | |----------------|---------|----------|----------------------------------------------------------------------| | tolerance | number | - | 必填,平滑容差(单位与坐标一致),值越大平滑程度越高 | | precision | number | 1e-6 | 浮点计算精度,避免极小值导致的计算误差 | | reuseBuffer | boolean | true | 是否复用输入缓冲区的空闲空间,开启可减少50%临时内存占用 |

返回值

  • Float32Array:平滑后的线数据,格式与输入一致([x0, y0, x1, y1, ...]

格式支持说明

| 模块格式 | 适用场景 | 引入方式 | 构建产物路径 | |----------|-----------------------------------|-------------------------------------------|-------------------------------| | ESM | 现代前端(Vite/Webpack 5/Rollup) | import { paek } from 'paek' | dist/paek.esm.js(未压缩) | | CJS | Node.js/旧前端(Webpack 4) | const { paek } = require('paek') | dist/paek.cjs.js(未压缩) | | IIFE | 浏览器无构建环境 | <script src="paek.iife.min.js"></script> | dist/paek.iife.min.js(压缩)|

注:所有产物均包含SourceMap,方便调试。

性能数据

测试环境:Node.js 18.17.0 | Intel i7-12700H | 32GB DDR4 | 数据规模 | 处理时间(v4.0) | 内存占用 | 相对基础版提升 | |----------|------------------|----------|----------------| | 1k 点 | 0.19ms | 0.02MB | 67.2% | | 10k 点 | 1.76ms | 0.28MB | 66.5% | | 100k 点 | 18.9ms | 2.9MB | 64.7% | | 1M 点 | 189.3ms | 29MB | 65.4% |

优化亮点

  1. SIMD加速:热点计算并行化,处理速度提升30%-50%
  2. 内存复用:缓冲区复用+紧凑数组,内存占用降低31%
  3. 动态核因子:根据容差自适应权重衰减,计算量减少10%-30%

兼容性

| 环境 | 支持版本 | 备注 | |---------------------|----------------|-------------------------------| | Node.js | ≥14.0.0 | 需启用ES6+支持 | | Chrome | ≥88.0 | 支持SIMD加速 | | Firefox | ≥85.0 | 部分版本需手动开启SIMD(about:config) | | Safari | ≥14.0 | SIMD加速暂不支持,自动降级 | | Edge | ≥88.0 | 同Chrome内核 |

常见问题

Q1:输入数据格式错误怎么办?

A:确保输入为Float32Array且长度为偶数(每个点含x/y),若输入无效(如<2个点),函数会直接返回输入副本,避免报错。

Q2:SIMD加速不生效?

A:检查环境是否支持SIMD(Chrome/Firefox最新版默认支持),不支持时会自动降级为普通实现,性能仍优于基础版。

Q3:如何平衡平滑精度与速度?

A:小容差(0.3-0.8)适合高精度场景(如GIS专业数据),大容差(1.0-3.0)适合快速平滑(如实时轨迹),可通过precision调整计算精度。

贡献指南

  1. Fork 仓库
  2. 创建特性分支(git checkout -b feature/xxx
  3. 提交代码(git commit -m 'feat: 添加xxx功能'
  4. 推送分支(git push origin feature/xxx
  5. 提交PR

许可证

MIT License