{"uri":"at://did:plc:gfrmhdmjvxn2sjedzboeudef/site.standard.document/isnothing","cid":"bafyreied7fklhwv7hb7rakchbcbtqoc2hg5wfvkrrfeckrmnxwg6qjehoa","value":{"path":"/isnothing","site":"at://did:plc:gfrmhdmjvxn2sjedzboeudef/site.standard.publication/3md7ylshxzk2y","tags":["blog"],"$type":"site.standard.document","title":"Nothingness in Julia","content":{"$type":"pub.leaflet.content","pages":[{"id":"8e7d5170-4492-4502-8743-208c0a5c4507","$type":"pub.leaflet.pages.linearDocument","blocks":[{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":138,"byteStart":126},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":155,"byteStart":142},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":200,"byteStart":188},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":236,"byteStart":227},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]}],"plaintext":"If you ever work in Julia, something you'll notice is that lots of people and the language server will recommend that you use isnothing(x) or x === nothing instead of the value comparison x == nothing. This is also true of the ismissing functionality for missing values."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":20,"byteStart":16},"features":[{"uri":"https://discourse.julialang.org/t/x-nothing-vs-x-isa-nothing/64546/13","$type":"pub.leaflet.richtext.facet#link"}]},{"index":{"byteEnd":60,"byteStart":56},"features":[{"uri":"https://stackoverflow.com/a/38638838/6149469","$type":"pub.leaflet.richtext.facet#link"}]}],"plaintext":"Good discussion here, and a great Stack Overflow answer here."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","plaintext":"How meaningful is this, though? I decided to do some benchmarking to see how much of a difference this makes."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":29,"byteStart":17},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":67,"byteStart":60},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":86,"byteStart":77},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":104,"byteStart":91},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":123,"byteStart":110},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":176,"byteStart":167},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":246,"byteStart":234},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]}],"plaintext":"The TLDR is that x == nothing isn't specialized to checking nothing, whereas isnothing and x === nothing are. x === nothing is a core language feature (I believe) and isnothing is compile time dispatched and is thus relatively quick. x == nothing is a value comparison and I think requires some extra stuff to happen on top."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":50,"byteStart":41},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":94,"byteStart":81},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":134,"byteStart":122},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]}],"plaintext":"There's a speed component to this too -- isnothing is the fastest on my machine, x === nothing is the second fastest, and x == nothing is the slowest."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":9,"byteStart":0},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]}],"plaintext":"isnothing:"}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.code","language":"julia","plaintext":"julia> @benchmark isnothing(x)\nBenchmarkTools.Trial: 10000 samples with 1000 evaluations.\n Range (min … max):  1.539 ns … 24.880 ns  ┊ GC (min … max): 0.00% … 0.00%\n Time  (median):     1.579 ns              ┊ GC (median):    0.00%\n Time  (mean ± σ):   1.589 ns ±  0.269 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%\n\n   ▁▃▁          ██\n  ▃███▄▃▂▂▂▂▂▂▂███▇▆▃▂▂▂▂▁▁▂▃▅▅▃▃▂▂▂▂▂▁▁▁▂▂▂▂▂▂▂▂▁▁▁▁▁▂▂▂▂▂▂ ▃\n  1.54 ns        Histogram: frequency by time         1.7 ns <\n\n Memory estimate: 0 bytes, allocs estimate: 0."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":13,"byteStart":0},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]}],"plaintext":"x === nothing:"}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.code","language":"julia","plaintext":"julia> @benchmark x === nothing\nBenchmarkTools.Trial: 10000 samples with 1000 evaluations.\n Range (min … max):  1.755 ns … 5.381 ns  ┊ GC (min … max): 0.00% … 0.00%\n Time  (median):     1.772 ns             ┊ GC (median):    0.00%\n Time  (mean ± σ):   1.791 ns ± 0.102 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%\n\n             █▃▄▃▁\n  ▂▂▂▂▂▂▃▃▄▆▇██████▆▅▄▃▂▂▁▂▂▂▂▂▂▂▁▁▁▁▁▁▂▂▂▂▂▂▃▃▇▅▆▇▇▇▆▆▄▄▃▃ ▃\n  1.76 ns        Histogram: frequency by time       1.82 ns <\n\n Memory estimate: 0 bytes, allocs estimate: 0."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":25,"byteStart":13},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]}],"plaintext":"The bad one, x == nothing:"}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.code","language":"julia","plaintext":"julia> @benchmark x == nothing\nBenchmarkTools.Trial: 10000 samples with 1000 evaluations.\n Range (min … max):  2.419 ns … 10.722 ns  ┊ GC (min … max): 0.00% … 0.00%\n Time  (median):     2.681 ns              ┊ GC (median):    0.00%\n Time  (mean ± σ):   2.693 ns ±  0.235 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%\n\n    █       ▂          ▁▁      ▅▄    ▁\n  ▂▄█▂▁▅▄▃▁▆██▂▁▅▇▅▁▂▄▅██▂▅▆▂▁▅██▂▁▂██▃▂▂▂▂▂▂▂▂▁▁▂▄▃▁▁▁▂▃▂▁▁ ▃\n  2.42 ns        Histogram: frequency by time        3.11 ns <\n\n Memory estimate: 0 bytes, allocs estimate: 0."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":77,"byteStart":68},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":94,"byteStart":81},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]}],"plaintext":"This is usually why the language server will recommend that you use isnothing or x === nothing instead."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.header","level":2,"plaintext":"Missing values"}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":80,"byteStart":73},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":80,"byteStart":61},"features":[{"uri":"https://docs.julialang.org/en/v1/manual/faq/#faq-nothing","$type":"pub.leaflet.richtext.facet#link"}]},{"index":{"byteEnd":139,"byteStart":132},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":218,"byteStart":211},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]}],"plaintext":"The same is generally true of missing values. Missing values differ from nothing in that they are used to represent missing data -- nothing is returned by default when a return value is not otherwise specified. missing is more for\ncases where you don't know a value, e.g. if you don't have data for an observation in a statistical model."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.text","facets":[{"index":{"byteEnd":94,"byteStart":81},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":110,"byteStart":98},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]},{"index":{"byteEnd":141,"byteStart":139},"features":[{"$type":"pub.leaflet.richtext.facet#code"}]}],"plaintext":"Interestingly, on Julia 1.10.2, the fastest is not one of the strict comparisons x === missing or ismissing(x), but a raw comparison using ==. Not really sure what's up with that, but whatever."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.code","language":"julia","plaintext":"julia> @benchmark x == missing\nBenchmarkTools.Trial: 10000 samples with 1000 evaluations.\n Range (min … max):  0.883 ns … 7.668 ns  ┊ GC (min … max): 0.00% … 0.00%\n Time  (median):     0.890 ns             ┊ GC (median):    0.00%\n Time  (mean ± σ):   0.896 ns ± 0.107 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%\n\n         ▁ ▇ ██ ▅ ▂\n  ▂▂▁▄▁▆▁█▁█▁██▁█▁█▁▇▁▆▁▅▄▁▄▁▃▁▃▁▃▁▃▃▁▃▁▃▁▂▁▂▁▂▂▁▂▁▂▁▂▁▂▁▂▂ ▃\n  0.883 ns       Histogram: frequency by time      0.914 ns <\n\n Memory estimate: 0 bytes, allocs estimate: 0."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.code","language":"julia","plaintext":"julia> @benchmark ismissing(x)\nBenchmarkTools.Trial: 10000 samples with 1000 evaluations.\n Range (min … max):  1.757 ns … 5.822 ns  ┊ GC (min … max): 0.00% … 0.00%\n Time  (median):     1.776 ns             ┊ GC (median):    0.00%\n Time  (mean ± σ):   1.783 ns ± 0.088 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%\n\n           ▁▁▂█    ▂█▁▁▁▁\n  ▂▂▂▂▂▃▆▆▇████▇▆▅███████▅▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▂▃▃▃▃▃▃▂▃▃▃▃▃▃▃ ▃\n  1.76 ns        Histogram: frequency by time       1.82 ns <\n\n Memory estimate: 0 bytes, allocs estimate: 0."}},{"$type":"pub.leaflet.pages.linearDocument#block","block":{"$type":"pub.leaflet.blocks.code","language":"julia","plaintext":"julia> @benchmark x === missing\nBenchmarkTools.Trial: 10000 samples with 1000 evaluations.\n Range (min … max):  1.540 ns … 4.822 ns  ┊ GC (min … max): 0.00% … 0.00%\n Time  (median):     1.554 ns             ┊ GC (median):    0.00%\n Time  (mean ± σ):   1.561 ns ± 0.081 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%\n\n           ▂▃▄▅▇▇█▇▅▄ ▃\n  ▂▂▂▃▄▅▅▇███████████▁█▇▅▃▃▂▂▂▂▂▁▁▁▁▁▁▁▁▁▂▂▂▂▂▃▂▃▃▃▃▃▃▃▃▃▃▃ ▄\n  1.54 ns        Histogram: frequency by time       1.59 ns <\n\n Memory estimate: 0 bytes, allocs estimate: 0."}}]}]},"publishedAt":"2024-04-03T07:00:00.000Z","textContent":"If you ever work in Julia, something you'll notice is that lots of people and the language server will recommend that you use isnothing(x) or x === nothing instead of the value comparison x == nothing. This is also true of the ismissing functionality for missing values.\n\nGood discussion here, and a great Stack Overflow answer here.\n\nHow meaningful is this, though? I decided to do some benchmarking to see how much of a difference this makes.\n\nThe TLDR is that x == nothing isn't specialized to checking nothing, whereas isnothing and x === nothing are. x === nothing is a core language feature (I believe) and isnothing is compile time dispatched and is thus relatively quick. x == nothing is a value comparison and I think requires some extra stuff to happen on top.\n\nThere's a speed component to this too -- isnothing is the fastest on my machine, x === nothing is the second fastest, and x == nothing is the slowest.\n\nisnothing:\n\nx === nothing:\n\nThe bad one, x == nothing:\n\nThis is usually why the language server will recommend that you use isnothing or x === nothing instead.\n\nMissing values\n\nThe same is generally true of missing values. Missing values differ from nothing in that they are used to represent missing data -- nothing is returned by default when a return value is not otherwise specified. missing is more for\ncases where you don't know a value, e.g. if you don't have data for an observation in a statistical model.\n\nInterestingly, on Julia 1.10.2, the fastest is not one of the strict comparisons x === missing or ismissing(x), but a raw comparison using ==. Not really sure what's up with that, but whatever."}}