| ▲ | kykat 2 hours ago | |||||||||||||||||||||||||
I saw something like that being suggested when working with GIS data with many points as classes in Java, the object overhead for storing XYZ doubles is quite crazy. The optimization was to build a global double array and use "pointers" to get and set the number in the array. Even JavaScript is much better for this, much, much better. | ||||||||||||||||||||||||||
| ▲ | spankalee an hour ago | parent | next [-] | |||||||||||||||||||||||||
JavaScript has the exact same issue - objects are on the heap and require allocation and pointer dereferencing. For huge collections of numbers, arrays might be better. But JS has another problem: there's no way to force a number to be unboxed (no primitive vs boxed types), so the array of doubles might very well be an array of pointers to numbers[1]. But with hidden class optimizations an object might be able to store a float directly in a field, so the array of objects will have one box per (x,y,z), while an array of "numbers" might have one box per number, so 3x as many. My guess is, without benchmarking, is that JS is much worse than Java then, because the "optimization" will end up being worse. [1]: Most JS engines have an optimization for small ints, called SMIs, that use pointer tagging to support either an int or a references, but I don't think they typically do this optimization for floats. | ||||||||||||||||||||||||||
| ▲ | lern_too_spel 2 hours ago | parent | prev [-] | |||||||||||||||||||||||||
You're describing array of structs vs. struct of arrays. Even in JavaScript, you would have to manually do the latter. | ||||||||||||||||||||||||||
| ||||||||||||||||||||||||||