覆盖变量或检查是否存在

时间:2015-05-22 11:13:23

标签: javascript performance variables

我在Javascript中制作一个简单的脚本,计算一些像最小值,最大值等的东西。我想知道以下哪个会更快,主要是为什么:

var x, y, z;
function test(){
if (x === undefined)
    x = Math.min(a, b);
if (y === undefined)
    y = a / b;
if (z === undefined)
    z = a - b;
    return [x, y, z];
}
test(); test(); test();

function test() {
    return [Math.min(a, b), a / b, a - b]
}

另外,我应该以第一种方式创建变量(仅在请求时定义变量),还是应该尽快定义变量,例如:在我的功能开始?我正在使用常量RGB值创建RGB到HSV脚本。 HSV并非总是被要求(用户要求)。

4 个答案:

答案 0 :(得分:1)

始终在顶部定义您的javascript变量,稍后再使用它们:

'use strict';

var a, b, c;

a = 1;

if (typeof b === 'undefined') {
    b = a || c;
}

// b is now 1

值得一提的是,如果你没有定义一个变量,它在首次使用时会变成全局变量(非常糟糕)。 在条件中定义变量(例如if)也是一种非常糟糕的做法。

答案 1 :(得分:0)

如果您不想更改x,y,z值(如果它们已经存在)那么检查它们是否存在然后过度写入将是更好的选择。

if (x === undefined)
    x = Math.min(a, b);
if (y === undefined)
    y = a / b;
if (z === undefined)
    z = a - b;

由于您正在使用RGB,这是最佳做法。 在开始时定义变量始终被认为是有用的,并且几乎不会对性能产生任何影响。

答案 2 :(得分:0)

如果未定义变量,请使用typeof。条件应该像

<?php 

$from = $_POST['email'];
$first_name= $_POST['first_name'];
$last_name= $_POST['last_name'];    
$email = $_POST['email'];
$mobile_no = $_POST['mobile_no'];
$applying_position = $_POST['apply_position'];

$upload_resume = $_POST['upload_resume'];

$target_path = "uploads/".$title;
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['upload_resume']['name']);
$_FILES['upload_resume']['tmp_name'];
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['upload_resume']['name']);
//echo $target_path;
$target_path=addslashes($target_path);
//echo $target_path;
if (isset($_REQUEST['submit']))
{
mysql_query("insert into careers_form(first_name,last_name,email,mobile_no,applying_position,upload_resume) 

VALUES('$first_name','$last_name','$email','$mobile_no','$applying_position','$target_path')") or die("ERROR:".mysql_error());
//echo "Record Added";
}
if(move_uploaded_file($_FILES['upload_resume']['tmp_name'], $target_path)) {
//echo "The file ". basename( $_FILES['upload_resume']['name'])." has been uploaded";
<!--$headers = "From:" . $from;
   $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($to1,$subject,$message,$headers);
-->
  echo "<div align=center><img src='img/thankyou.png' alt='error'/></div>";
} else{
echo "There was an error uploading the file, please try again!";
}
//$message = $first_name . " " . $last_name . " Careers Details:" . "\n\n".$upload_resume;

$tmpName = $_FILES['upload_resume']['tmp_name'];
echo $tmpName;
$fileType = $_FILES['upload_resume']['type'];
$fileName = $_FILES['upload_resume']['name'];

/* Start of headers */
$headers = "From: $from";

  $to = "jyothi_dadi123@yahoo.com";
  $subject = "This is subject";
  $message = "This is test message.";
if (file($tmpName)) {
  # Open a file
  $file = fopen($tmpName,'rb');
  $data = fread($file,filesize($tmpName));
  fclose($file);
  $randomVal = md5(time());
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

  /* Header for File Attachment */
  $headers .= "\nMIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed;\n" ;
  $headers .= " boundary=\"{$mimeBoundary}\"";

  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.\n\n" .
  "--{$mimeBoundary}\n" .
  "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  "Content-Transfer-Encoding: 7bit\n\n" .
  $message . "\n\n";

  /* Encoding file data */
  $data = chunk_split(base64_encode($data));

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" .
  "Content-Type: {$fileType};\n" .
  " name=\"{$fileName}\"\n" .
  "Content-Transfer-Encoding: base64\n\n" .
  $data . "\n\n" .
  "--{$mimeBoundary}--\n";
}

$flgchk = mail ("$to", "$subject", "$message", "$headers");

mysql_close();

?>

答案 3 :(得分:0)

完全依赖于上下文是否要覆盖现有值。如果你想要执行操作,无论是否存在变量,那么最好的方法是最好的。

x = Math.min(a, b);
y = a / b;
z = a - b;

这种方式更快,因为在第一种方式中你只是检查变量是未定义的,如果未定义则执行任务。最好直接执行任务。

希望这会对你有所帮助。