function measure(items) {
const startTime = new Date().getTime()
for(let i = 0;i < 500000;i++) {
items[0] === "Yoshi"
items[1] === "kuma"
items[2] === "Hattori"
}
return new Date().getTime() - startTime
}
function createNegativeArrayProxy(array) {
if (!Array.isArray(array)) {
throw new TypeError('Expected an Array')
}
return new Proxy(array, {
get: (target, index) => {
index = +index
return target[index < 0 ? target.length + index : index]
},
set: (target, index, val) => {
index =+ index
return
target(index < 0 ? target.length + index : index) = val
}
})
}
const ninjas = ["Yoshi", "Kuma", "Hattori"]
const proxiedNinjas = createNegativeArrayProxy(ninjas)
console.log("prox are round", Math.round(measure(proxiedNinjas)/measure(ninjas)))
console