Referring to the following SE answer.
When writing
A = A ?? B;
it is the same as
if( null != A ) A = A; else A = B;
Does that mean that
if( null == A ) A = B;
would be preferred, performance wise?
Or can I assume that the compiler optimizes the code when the same object is in the ??
notation?
The JavaScript double question mark (??) operator is called the nullish coalescing operator and it provides a default value when a variable or an expression evaluates to null or undefined.
The double question marks (??) are also called nullish coalescing operators and they allow to use of a default value set on the right side of the operator in case the initial value from the left side of the operator is null or undefined .
Double question marks(??) or nullish coalescing operator helps us to assign default values to null or undefined variables in Angular and Typescript. It's often called as Null coalescing operator.
In simplest way, two question marks are called "Coalescing Operator", which returns first non null value from the chain. e.g if you are getting a values from a nullable object, in a variable which is not nullable, then you can use this operator.
Don't worry about the performance, it will be negligible.
If you are curious about it, write some code to test the performance using Stopwatch
and see. I suspect you'll need to do a few million iterations to start seeing differences though.
You can also never assume about the implementation of things, they are liable to change in future - invalidating your assumptions.
My assumption is the performance difference is likely very, very small. I'd go for the null coalescing operator for readability personally, it is nice and condense and conveys the point well enough. I sometimes do it for lazy-load checking:
_lazyItem = _lazyItem ?? new LazyItem();
Although performance for ??
is negligible, the side effect sometimes may not be negligible. Consider the following program.
using System; using System.Diagnostics; using System.Threading; namespace TestProject { class Program { private string str = "xxxxxxxxxxxxxxx"; public string Str { get { return str; } set { if (str != value) { str = value; } // Do some work which take 1 second Thread.Sleep(1000); } } static void Main(string[] args) { var p = new Program(); var iterations = 10; var sw = new Stopwatch(); for (int i = 0; i < iterations; i++) { if (i == 1) sw.Start(); if (p.Str == null) { p.Str = "yyyy"; } } sw.Stop(); var first = sw.Elapsed; sw.Reset(); for (int i = 0; i < iterations; i++) { if (i == 1) sw.Start(); p.Str = p.Str ?? "yyyy"; } sw.Stop(); var second = sw.Elapsed; Console.WriteLine(first); Console.WriteLine(second); Console.Write("Ratio: "); Console.WriteLine(second.TotalMilliseconds / first.TotalMilliseconds); Console.ReadLine(); } } }
Run result on my PC.
00:00:00.0000015 00:00:08.9995480 Ratio: 5999698.66666667
Because there is an extra assignment using ??
, and the performance of the assignment sometimes might not guaranteed. This might lead to a performance issue.
I would rather use if( null == A ) A = B;
instead of A = A ?? B;
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With