Increase speed creating new array

Hi all,
In some places new array creates as a Array(). If we replace it to a [], we increase performance about 1.7 times:

def old_slow

Array(123)

end

def new_fast

[123]

end

Benchmark.ips do |x|

x.report(“new_fast”) { new_fast }

x.report(“old_slow”) { old_slow }

x.compare!

end

Calculating -------------------------------------

new_fast 184.920k i/100ms

old_slow 150.931k i/100ms


new_fast 7.528M (± 4.4%) i/s - 37.724M

old_slow 4.408M (± 4.3%) i/s - 22.036M

Comparison:

new_fast: 7528299.2 i/s

old_slow: 4408283.1 i/s - 1.71x slower

What do you think about it?

Andrey,

Be careful with this because the sematics of the two are not exactly equal:

[1] pry(main)> Array([123])

=> [123]

[2] pry(main)> [[123]]

=> [[123]]

and the desired sematics in the locations that you link to seem to be the Array() variant where the input might be a scalar or an array itself.

Thanks for your feedback.
Yes, I will need find places where args passes to Array() is not kind of Array class.