4 ms·
Ruby's `Pathname#absolute?` is doing a lot more work[1]. I don't know why, but it is. If I change it to just check the first character for the file separator,
by InAnEmergency 10y ago
Ruby's `Pathname#absolute?` is doing a lot more work[1]. I don't know why, but it is.
If I change it to just check the first character for the file separator, as I understand FasterPath does, then of course it is faster:
Warming up --------------------------------------
original 4.109k i/100ms
faster 110.202k i/100ms
Calculating -------------------------------------
original 42.645k (± 3.7%) i/s - 213.668k in 5.017595s
faster 2.643M (± 4.4%) i/s - 13.224M in 5.013294s
Comparison:
faster: 2643274.6 i/s
original: 42644.5 i/s - 61.98x slower
My implementation:
class Pathname
def f_relative?
@path[0] != File::SEPARATOR
end
end
[1] https://github.com/ruby/ruby/blob/68ebbbfebe5b666cf76ab41f1e6191a172d62690/ext/pathname/lib/pathname.rb#L227 https://github.com/ruby/ruby/blob/68ebbbfebe5b666cf76ab41f1e...
- 33degrees 10y agoA quick look at the tests[1] shows that it handles drive letters. The current implementation is probably not the most efficient though... [1] https://github.com/ruby/ruby/blob/68ebbbfebe5b666cf76ab41f1e6191a172d62690/test/pathname/test_pathname.rb#L276 https://github.com/ruby/ruby/blob/68ebbbfebe5b666cf76ab41f1e...
- munificent 10y agoThat returns a wrong result on Windows paths, and throws an exception on the empty string. There are a ton of edge cases in path manipulation code.
- kevincox 10y agoI'm pretty sure his implementation works on empty strings because `str[0].nil?`. So the comparison is false, giving the right (?) answer.