Skip to content

Use slice appending instead of insertion at zeroth index in "stack array" implementation #543

Open
@BulkBeing

Description

@BulkBeing

Description
Current stack array implementation stores the last inserted value at zeroth index of the backing slice. This is inefficient as each push operation will require a new allocation and copying of contents in the current slice.

func stackPush(n any) {
	stackArray = append([]any{n}, stackArray...)
}

Inserting latest element to the last index (append) will also help in reusing existing slices's remaining capacity when multiple push and pop operations happens in mixed order.
Also, the current implementation uses a global variable as the backing slice. It should be implemented as an object and global mutable variables should be avoided.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions