重构Javascript以优化速度

重构Javascript以优化速度,javascript,optimization,refactoring,Javascript,Optimization,Refactoring,我能够找到一个有效的解决方案,但我想知道如何重构代码,使其更快。多谢各位 /* 给定一个整数序列作为数组,确定它是否为 通过不再移除,可以获得严格递增的序列 数组中的多个元素。 */ //变量array2=[0,-2,5,6];//期待真实 //var array2=[1,1,1,2,3];//期望错误 var array2=[1,2,5,5,5]//预期为false 函数几乎递增序列(序列){ 让传递=假; sequence.forEach((数字、idx、数组)=>{ 让sequence2

我能够找到一个有效的解决方案,但我想知道如何重构代码,使其更快。多谢各位

/*
给定一个整数序列作为数组,确定它是否为
通过不再移除,可以获得严格递增的序列
数组中的多个元素。
*/
//变量array2=[0,-2,5,6];//期待真实
//var array2=[1,1,1,2,3];//期望错误
var array2=[1,2,5,5,5]//预期为false
函数几乎递增序列(序列){
让传递=假;
sequence.forEach((数字、idx、数组)=>{
让sequence2=sequence.filter((num,id)=>id!==idx);
让answer=sequence2.every((num,idx,array)=>{
返回idx==sequence2.length-1 | | numlog(几乎递增顺序(array2))如果我对问题的理解正确,您只需要在序列中传递1次。您正在寻找索引(i-1)和(i)之间的差异在整个数组中仅减少一次的数组

(此代码经过多次编辑以处理边缘情况)

函数几乎递增序列(序列){
if(sequence.length==1)
返回true;
var=0;
var i=-1;
对于(var索引=1;索引序列[索引])
{
countreduces++;
i=指数;
如果(计数减少>1)
返回false;
}
}
var canRemovePreviousElement=(i==1 | |序列[i-2]序列[i+1]);
如果(可以删除以前的元素)
返回cannotRemoveSelectedElement;
返回值(countDegress==1&&!cannotRemoveSelectedElement);
}
//测试/////////////////////////////////
var passTestCases={
“删除索引0:[2,0,1,2],
“删除索引1:[0,1,0,0,1],
“删除索引(最后一个-1)”:[0,1,-1,2],
“删除索引(上次)”:[0,1,2,-1],
“仅删除元素”:[1]
};
var failTestCases={
“删除索引0或1:[0,-1,1,2],
“删除所有增加的索引”:[1,2,5,5,5],
“删除任何索引,并减少”:[1,0],
“一次减少”:[0,1,2,0,1],
“两次减少”:[1,0,2,1],
“没有要删除的元素”:[]
};
运行测试(passTestCases,true,几乎递增的序列);
运行测试(failTestCases、false、几乎递增的序列);
函数运行测试(测试用例、expectedResult、方法){
console.log(“\n应返回”+expectedResult);
for(测试用例中的var键)
{
var testCase=testCases[key];
var结果=方法(testCase);
log(key+”:“+testCase+”:result“+result);
}
}
关于速度优化:

此方法只通过数组一次,而原始post使用嵌套循环。一般来说,对于大型数据集,线性算法比非线性算法要快


将这两种算法的速度与这些微小的测试用例进行比较,得出了不确定的结果。

请注意,当[i]重构=可读性优化=可读性优化时,我们存在冲突speed@dustytrash:refactoring==为实现另一个目的而进行的重组。可读性和速度都是目的。要求“删除不超过一个元素”,因此:return(countreduces@Piero,但示例中说[1,2,5,5,5]应该返回false。@Macrunning“sequence.forEach”、“sequence.filter”和“sequence.every”都是循环。它们和“for”之间没有特别的区别loop。您的示例中的循环是嵌套循环。与单个循环相比,这会导致性能降低。我想我正在尝试了解for循环中的何处,如果需要检查几乎递增的序列,它会删除元素。这不是在检查数组中的所有元素吗?感谢您通过了所有测试。现在尝试继续ss.:)如果(prev>0&&sequence[i]@Macrunning添加了一些解释,你能解释一下这部分吗?
。至于[10,1,2,3,4,5],当prev=0时,我们不使用continue,但我们不返回false,对于删除的==false条件,返回false是else^^
function almostIncreasingSequence(sequence) {
    if(sequence.length == 1)
        return true;

    var countDecreases = 0;
    var i = -1;
    for(var index=1; index<sequence.length; index++)
    {
        if(sequence[index-1] > sequence[index])
        {
            countDecreases++;
            i = index;
            if(countDecreases > 1)
                return false;
        }
    }
    var canRemovePreviousElement = (i == 1 || sequence[i-2] <= sequence[i]);
    var cannotRemoveSelectedElement = (i+1 < sequence.length && sequence[i-1] > sequence[i+1]);

    if(canRemovePreviousElement)
        return cannotRemoveSelectedElement;

    return (countDecreases == 1 && !cannotRemoveSelectedElement);
}

// Testing /////////////////////////////////

var passTestCases = {
    "remove index 0": [2, 0, 1, 2],
    "remove index 1": [0, 1, 0, 0, 1],
    "remove index (last-1)": [0, 1, -1, 2],
    "remove index (last)": [0, 1, 2, -1],
    "remove only element": [1]
};
var failTestCases = {
    "remove index 0 or 1": [0, -1, 1, 2],
    "remove any index, all increasing": [1, 2, 5, 5, 5],
    "remove any index, with decrease": [1, 0],
    "one decrease": [0, 1, 2, 0, 1],
    "two decreases": [1, 0, 2, 1],
    "no elements to remove": []
};

runTests(passTestCases, true, almostIncreasingSequence);
runTests(failTestCases, false, almostIncreasingSequence);

function runTests(testCases, expectedResult, method) {
    console.log("\nShould Return " + expectedResult);
    for(var key in testCases)
    {
        var testCase = testCases[key];
        var result = method(testCase);
        console.log(key + ": " + testCase + ": result " + result); 
    }
}