reassign or extend string

Benchmark created on


Setup

class ExtendedArray extends Array {
    constructor(items) {
        super(items)
    }

    get last() {
        return this[this.length - 1]
    }

    addToLastWithGetter(text) {
        this.last.text ??= ''
        this.last.text += text
    }

    addToLast(text) {
        this[this.length - 1].text ??= ''
        this[this.length - 1].text += text
    }

    concatToLast(text) {
        this[this.length - 1].text ??= ''
        this[this.length - 1].text = this[this.length - 1].text + text
    }

    templateStringToLast(text) {
        this[this.length - 1].text ??= ''
        this[this.length - 1].text = `${this[this.length - 1].text}${text}`
    }

    replaceLast(text) {
        const last = this[this.length - 1]
        this[this.length - 1] = {
            text: last.text + text,
        }
    }
}

let addToLastWithGetter = new ExtendedArray({text: 'x'})
let addToLast = new ExtendedArray({text: 'x'})
let concatToLast = new ExtendedArray({text: 'x'})
let templateStringToLast = new ExtendedArray({text: 'x'})
let replaceLast = new ExtendedArray({text: 'x'})

Test runner

Ready to run.

Testing in
TestOps/sec
addToLastWithGetter
addToLastWithGetter.addToLastWithGetter("y")
ready
addToLast
addToLast.addToLast("y")
ready
concatToLast
concatToLast.concatToLast("y")
ready
templateStringToLast
templateStringToLast.templateStringToLast("y")
ready
replaceLast
replaceLast.replaceLast("y")
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.