reassign or extend string (v2)

Revision 2 of this 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}`
    }

    replaceWithTemplateStringToLast(text) {
        const last = this[this.length - 1]
        this[this.length - 1] = {
            text: `${last.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 replaceWithTemplateStringToLast = new ExtendedArray({text:'x'})
let replaceLast = new ExtendedArray({text: 'x'})

Test runner

Ready to run.

Testing in
TestOps/sec
addToLastWithGetter
addToLastWithGetter.addToLastWithGetter("yasdfasdfasdfasdfasdfsd fasdf asdf as f")
ready
addToLast
addToLast.addToLast("yasdfasdfasdfasdfasdfsd fasdf asdf as f")
ready
concatToLast
concatToLast.concatToLast("yasdfasdfasdfasdfasdfsd fasdf asdf as f")
ready
templateStringToLast
templateStringToLast.templateStringToLast("yasdfasdfasdfasdfasdfsd fasdf asdf as f")
ready
replaceLast
replaceLast.replaceLast("yasdfasdfasdfasdfasdfsd fasdf asdf as f")
ready
replaceWithTemplateStringToLast
replaceWithTemplateStringToLast.replaceWithTemplateStringToLast("yasdfasdfasdfasdfasdfsd fasdf asdf as f")
ready

Revisions

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