Hacker News new | past | comments | ask | show | jobs | submit login

If an array is the hovercraft, it can hold 4 billion "things" regardless of what those things are! So you need to fill that up to meet the requirements of the saying.



And if a hovercraft is an array of size 331, then it should hold 331 eels.


Ah but now we're at the crux of the issue; in Javascript, hovercrafts grow in size to accommodate as many eels as you put in, but to a maximum of ~4 billion. So "full" is not a question you can ask of your hovercraft, though it will tell you once it's overflowing.


Now there's a thought!

What if we used a storage method that didn't grow?

In JavaScript we have typed arrays, we could use a `Uint16Array` to store UTF-16 code points as integers in each element, then we could define the amount of eels that our "hovercraft" could store ahead of time in a way that it won't expand.

  const hovercraftEelCapacity = 331 // We can define our hovercraft to have room for 331 eels
  const eel = 'eel'
  const hovercraft = new Uint16Array(new ArrayBuffer(2 * eel.length * hovercraftEelCapacity)) // Each "eel" takes up 3 16-bit integers

  for (let i = 0; i < hovercraftEelCapacity * eel.length; i += eel.length) {
    hovercraft[i] = eel.codePointAt(0)
    hovercraft[i+1] = eel.codePointAt(1)
    hovercraft[i+2] = eel.codePointAt(2)
  }

  console.log('Your hovercraft full of eels: ', hovercraft)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: