refactor(filters): simplify join output-size accounting

Sum stringified element lengths in a single pass and keep the guarded
Array.prototype.join for the result, instead of building an intermediate
parts array.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
Yang Jun
2026-07-06 20:37:34 +08:00
co-authored by Cursor
parent 4ef0aa3fe7
commit a0103af11d
+3 -9
View File
@@ -8,16 +8,10 @@ import { EmptyDrop } from '../drop'
export const join = argumentsToValue(function (this: FilterImpl, v: any[], arg: string) {
const array = toArray(v)
const sep = isNil(arg) ? ' ' : stringify(arg)
const parts: string[] = []
let outputSize = array.length > 0 ? sep.length * (array.length - 1) : 0
for (let i = 0; i < array.length; i++) {
const item = array[i]
const part = isNil(item) ? '' : String(item)
outputSize += part.length
parts.push(part)
}
let outputSize = sep.length * Math.max(array.length - 1, 0)
for (let i = 0; i < array.length; i++) outputSize += String(array[i]).length
this.context.memoryLimit.use(outputSize)
return parts.join(sep)
return Array.prototype.join.call(array, sep)
})
export const last = argumentsToValue(function (this: FilterImpl, v: any) {
return isArrayLike(v) ? readArrayElement(v, -1, this.context.ownPropertyOnly) : ''