index.d.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Like `Array#splice`, but smarter for giant arrays.
  3. *
  4. * `Array#splice` takes all items to be inserted as individual argument which
  5. * causes a stack overflow in V8 when trying to insert 100k items for instance.
  6. *
  7. * Otherwise, this does not return the removed items, and takes `items` as an
  8. * array instead of rest parameters.
  9. *
  10. * @template {unknown} T
  11. * Item type.
  12. * @param {Array<T>} list
  13. * List to operate on.
  14. * @param {number} start
  15. * Index to remove/insert at (can be negative).
  16. * @param {number} remove
  17. * Number of items to remove.
  18. * @param {Array<T>} items
  19. * Items to inject into `list`.
  20. * @returns {undefined}
  21. * Nothing.
  22. */
  23. export function splice<T extends unknown>(
  24. list: T[],
  25. start: number,
  26. remove: number,
  27. items: T[]
  28. ): undefined
  29. /**
  30. * Append `items` (an array) at the end of `list` (another array).
  31. * When `list` was empty, returns `items` instead.
  32. *
  33. * This prevents a potentially expensive operation when `list` is empty,
  34. * and adds items in batches to prevent V8 from hanging.
  35. *
  36. * @template {unknown} T
  37. * Item type.
  38. * @param {Array<T>} list
  39. * List to operate on.
  40. * @param {Array<T>} items
  41. * Items to add to `list`.
  42. * @returns {Array<T>}
  43. * Either `list` or `items`.
  44. */
  45. export function push<T extends unknown>(list: T[], items: T[]): T[]