Javascript代码效率
我不确定在这里问这个是否合适。如果没有,就投票否决我。但是我想对我的纵横比函数的效率发表意见。以下是一个用于确定纵横比和限制图像大小的函数。我做得怎么样Javascript代码效率,javascript,jquery,Javascript,Jquery,我不确定在这里问这个是否合适。如果没有,就投票否决我。但是我想对我的纵横比函数的效率发表意见。以下是一个用于确定纵横比和限制图像大小的函数。我做得怎么样 function constrainTwoNumbers(options){ d = { dir: 'either', // direction: 'auto', 'vertical' or 'horizontal'. What side of the image do you want to constrain? orgw:
function constrainTwoNumbers(options){
d = {
dir: 'either', // direction: 'auto', 'vertical' or 'horizontal'. What side of the image do you want to constrain?
orgw:0,
orgh:0,
target:100,
}
// merge the options with the default values
o = $.extend(d, options);
// create object to write results into
var result = [];
switch(o.dir){
case 'either':
// no direction is set, limit the largest side.
// determine what the orientation is.
if(o.orgw > o.orgh){ //landscape
aspect = o.orgw / o.target;
}else if(o.orgw < o.orgh){ //portrait
aspect = o.orgh / o.target;
}else if(o.orgw === o.orgh){ // the image is square. Just pass both dimensions as targeted
result.w = o.target;
result.h = o.target;
return result;
}
break;
case 'horizontal':
aspect = o.orgw / o.target;
break;
case 'vertical':
aspect = o.orgh / o.target;
break;
}
result.w = Math.round(o.orgw / aspect);
result.h = Math.round(o.orgh / aspect);
return result;
}
函数约束两个数字(选项){
d={
dir:'任意',//方向:'自动','垂直'或'水平'。要约束图像的哪一侧?
orgw:0,
组织:0,
目标:100,
}
//将选项与默认值合并
o=$.extend(d,选项);
//创建要将结果写入的对象
var结果=[];
开关(o.dir){
“任一”情况:
//未设置方向,请限制最大边。
//确定方向是什么。
如果(o.orgw>o.orgh){//
方面=o.orgw/o.target;
}如果(o.orgw
如果/else
function constrainTwoNumbers(options){
var d = {
dir: 'either', // direction: 'either', 'vertical' or 'horizontal'. What side of the image do you want to constrain?
orgw:0,
orgh:0,
target:100
};
// merge the options with the default values
var o = $.extend(d, options);
// create object to write results into
var result = [];
if ((o.dir === 'either' && o.orgw > o.orgh) || (o.dir === 'horizontal'))
{
aspect = o.orgw / o.target;
}
else
{
aspect = o.orgh / o.target;
}
result.w = Math.round(o.orgw / aspect);
result.h = Math.round(o.orgh / aspect);
return result;
}
您可以使用来检查性能
以及查找内存泄漏和其他错误。需要将您的注释改为“要么”而不是“自动”。看起来没问题-您真的对算法的速度有问题吗?我没有问题,只是渴望改进。