Performance benefits of a static empty array instance

前端 未结 4 1663
夕颜
夕颜 2020-12-11 17:47

It seems common practice to extract a constant empty array return value into a static constant. Like here:

public class NoopParser implements Parser {
    pr         


        
4条回答
  •  醉酒成梦
    2020-12-11 18:12

    I will go out on a limb and say that the performance benefit, even though using constant is much faster, is not actually relevant; because the software will likely spend a lot more time in doing other things besides returning empty arrays. If the total run-time is even hours a few extra seconds spent in creating an array does not mean much. By the same logic, memory consumption is not relevant either.

    The only reason I can think of for doing this is readability.

提交回复
热议问题