所以我们今天在公司讨论了+new Date()
是不是很好的做法。
有些人比new Date().getTime()
更喜欢这种方式。
在我看来,这很方便,但在另一方面,人们会说它更难阅读。
除了明显的“对于不熟悉一元算子的人更难理解”之外,是否有任何利弊?
答案 0 :(得分:48)
getTime
方法似乎是huge amount faster:
为什么会这样?
以下是您在getTime
个实例上调用Date
方法时会发生的情况:
[[PrimitiveValue]]
内部属性的值。以下是将一元加运算符应用于Date
实例时会发生的情况:
Date
个实例的答案 1 :(得分:7)
我发现这种类型的代码往往不经常被调用,所以我认为最好为你通常看到的内联用法添加测试:
e.g。
var t = (new Date()).getTime();
和
var t = +new Date();
JSPerf结果显示这两者在速度上差别不大:http://jsperf.com/get-time-vs-unary-plus/7
先前的perf结果的问题是该示例不实用。你不会在实践中继续获得相同的now
。如果现在没有改变,你只需存储一次getTime()
结果。正如这些新结果所示,在典型使用情况下速度差异并不显着。
所以我想一般的建议是,使用一次性使用+new Date()
更短,但(new Date()).getTime()
更具可读性(并且更快)。
如果您要使用较新的Date.now()
,则需要实施建议的填充程序以支持here
if (!Date.now) {
Date.now = function now() {
return new Date().getTime();
};
}
(虽然我很疑惑他们为什么不在这个例子中使用匿名函数)
答案 2 :(得分:7)
在经历了这么多年的考虑之后,我得出的结论是,绩效不是这里的一个因素。
所以这就是我在可读性方面更喜欢的解决方案:
Date.now();
答案 3 :(得分:1)
(new Date()).getTime();

答案 4 :(得分:0)
这完全取决于你所做的比较。
事实上,在同一个日期对象上连续运行.getTime一百万次,与读取一百万次固定变量一样快,这似乎与任何实际代码无关。
更有趣的测试可能会比较每次迭代中从新日期返回时间字符串所需的时间。
<!doctype html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>get time</title>
<script>
/*does n= +new Date() take longer than n= new Date().getTime()?*/
var score=[],runs;
function tests(arg){
runs= parseInt(document.getElementsByTagName('input')[0].value)|| 100000;
var A= [], i= runs, start= new Date(),n=1357834972984;
while(i--){
A.push(n);
}
if(arg!==true){
score[0]= (new Date()- start);
setTimeout(tests0, 0);
}
}
function tests0(){
runs= parseInt(document.getElementsByTagName('input')[0].value)|| 100000;
var A= [], i= runs, start= new Date();
while(i--){
A.push(+(start));
}
score[1]= (new Date()- start);
setTimeout(tests1, 0);
}
function tests1(){
var A= [], i= runs, start= new Date();
while(i--){
A.push(start.getTime());
}
score[2]= (new Date()- start);
setTimeout(tests2, 0);
}
function tests2(){
var A= [], i= runs, start= new Date();
while(i--){
A.push(+(new Date));
}
score[3]= (new Date()- start);
setTimeout(tests3, 0);
}
function tests3(){
var A= [], i= runs, start= new Date();
while(i--){
A.push(new Date().getTime())
}
score[4]= (new Date()- start);
setTimeout(report, 0);
}
function report(){
var inp=document.getElementsByTagName('input'),t,
lab=document.getElementsByTagName('label')
for(var i=0;i<5;i++){
inp[i+1].value=score[i]+' msec';
}
}
onload= function(){
tests(true);
document.getElementsByTagName('button')[0].onclick=tests;
}
</script>
</head>
<body>
<h1>Comparing +prefix and getTime()</h1>
<p>
This comparison builds an array of the values for each test case, eg, 100000 array items for each case.
</p>
<ol>
<li>Building an array of a fixed integer, no date calculations at all.</li>
<li>Reading +prefix of existing Date and adding each value to array.</li>
<li>Reading getTime from existing Date and adding each value to array.</li>
<li>Creating a new date with +(new Date) and adding each value to array.</li>
<li>Creating a new date with new Date().getTime()and adding each value to array.</li>
</ol>
<p><label>Iterations of each test:<input type="text" size="8" value="100000"></label>
<button type="button">Run Tests</button></p>
<p><label>1. Building the array with no calculation: <input type="text" size="12"></label></p>
<h2>Repeatedly reading the same created date</h2>
<p><label>2. +prefix to existing Date: <input type="text" size="12"></label></p>
<p><label>3. getTime from existing Date: <input type="text" size="12"></label></p>
<h2>Creating a new date and reading new value each time:</h2>
<p><label>4. +(new Date): <input type="text" size="12"></label></p>
<p><label>5. new Date().getTime(): <input type="text" size="12"></label></p>
</body>
</html>