Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefer
Array.new(n) {}
to n.times.map {}
* It is quite a bit faster: ruby -rbenchmark/ips -e 'Benchmark.ips { |x| x.report("times.map") { 1000.times.map {} }; x.report("Array.new") { Array.new(1000) {} }; x.compare! }' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux] Warming up -------------------------------------- times.map 976.000 i/100ms Array.new 1.641k i/100ms Calculating ------------------------------------- times.map 9.808k (± 0.3%) i/s - 49.776k in 5.075013s Array.new 16.601k (± 1.0%) i/s - 83.691k in 5.041970s Comparison: Array.new: 16600.8 i/s times.map: 9808.2 i/s - 1.69x slower
- Loading branch information