Optimized array manipulation utilities for giant arrays without V8 stack overflow
tessl install tessl/npm-micromark-util-chunked@2.0.0Optimized array manipulation utilities specifically designed to handle giant arrays without causing V8 stack overflows. This package provides essential performance optimizations for micromark ecosystem extensions that need to process large amounts of data in parsing and tokenization scenarios.
npm install micromark-util-chunkedimport {push, splice} from 'micromark-util-chunked';For environments with ESM.sh:
import {push, splice} from 'https://esm.sh/micromark-util-chunked@2';import {push, splice} from 'micromark-util-chunked';
// Efficiently append items to a large array
const events = [];
const newEvents = [['enter', token, context], ['exit', token, context]];
const result = push(events, newEvents);
// Safely splice large arrays without stack overflow
const largeArray = new Array(50000).fill().map((_, i) => i);
const itemsToInsert = new Array(20000).fill('new-item');
splice(largeArray, 1000, 5000, itemsToInsert);Efficiently appends items to arrays with performance optimizations for empty lists and batch processing to prevent V8 engine issues.
/**
* Append items (an array) at the end of list (another array).
* When list was empty, returns items instead.
*
* @param {Array<T>} list - List to operate on
* @param {Array<T>} items - Items to add to list
* @returns {Array<T>} Either list or items
*/
function push(list, items);Key Features:
items directly when list is empty for performanceSafe array splicing for giant arrays that prevents V8 stack overflow by chunking operations when dealing with large datasets.
/**
* Like Array#splice, but smarter for giant arrays.
* Does not return removed items and takes items as array instead of rest parameters.
*
* @param {Array<T>} list - List to operate on
* @param {number} start - Index to remove/insert at (can be negative)
* @param {number} remove - Number of items to remove
* @param {Array<T>} items - Items to inject into list
* @returns {undefined} Nothing
*/
function splice(list, start, remove, items);Key Features:
Array#splice for large operationsUsage Examples:
import {splice} from 'micromark-util-chunked';
// Insert many items at the beginning
const items = new Array(15000).fill('data');
const insertItems = new Array(5000).fill('new-data');
splice(items, 0, 0, insertItems);
// Replace items in the middle with negative indexing
splice(items, -1000, 500, ['replacement1', 'replacement2']);
// Remove items without replacement
splice(items, 100, 50, []);This utility is designed for scenarios where arrays can grow to hundreds of thousands of elements:
constants.v8MaxSafeChunkSize from micromark-util-symbol)"type": "module")