mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
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:
@@ -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) : ''
|
||||
|
||||
Reference in New Issue
Block a user