I was solving a challenge which involves some looping and computation of new array. My solution worked well for fairly small arrays but it failed on big ones with 10k+ items. I was getting a "timeout error"
Then I have implemented my own array like:
class MyArray {
constructor(initialArray) {
this.length = initialArray.length
this.data = initialArray
}
get(index) {
return this.data[index]
}
push(item) {
this.data[this.length] = item
this.length++
return this.data
}
pop() {
const lastItem = this.data[this.length - 1]
delete this.data[this.length - 1]
this.length--
return lastItem
}
/// etc
}
Then I initiated this with given array and used my arrays methods to perform the calculations and it worked even with big arrays given for the assignment.
I still don't really understand why this is more performant and faster? Since I'm using native JavaScript Arrays methods on my new Array class methods...
I would appreciate more clarification on it.