I tried the following snippets in the consoles of Safari (8.0.5), Chrome (42.0.2311.90), Firefox (34.0.5) and Opera (29.0.1795.47):
With !!
:
var time = new Date().getTime();
for(var i = 0; i < 1000000; i++)
{
var a = true;
a = !!a;
}
time = new Date().getTime() - time;
console.log(time);
With Boolean()
:
var time = new Date().getTime();
for(var i = 0; i < 1000000; i++)
{
var a = true;
a = Boolean(a);
}
time = new Date().getTime() - time;
console.log(time);
No matter what I set a
to before using !!
or Boolean
, in all browsers !!
was faster.
In addition to this (as Bergi already said in his answer), Boolean
is just a variable that has to be resolved and can be overwritten, while !!
cannot be.
Boolean()
is more reader-friendly though.
So what is actually better is a question of opinion, but I would say performance and safety together outweigh readability, so I prefer !!
.