简体   繁体   English

用于处理 javascript 中相当大的数组的函数式或命令式代码?

[英]Functional or Imperative code for dealing with considerably large array in javascript?

When working with array in javascript what would you choose, functional way or imperative way while imperative is faster than functional.在 javascript 中使用数组时,您会选择什么,函数式方式还是命令式方式,而命令式比函数式更快。 I am so confused.我感到很困惑。

Here is the jsPerf test i ran with plain for loop and pair of map and filter.这是我使用普通 for 循环和一对映射和过滤器运行的 jsPerf 测试

My two cents: In general, I prefer working functional way with arrays, since I find functional methods more flexible and easier to read and maintain.我的两分钱:总的来说,我更喜欢使用数组的函数式方法,因为我发现函数式方法更灵活,更易于阅读和维护。

Especially where the performance are not critical or the there aren't noticeable differences.特别是在性能不重要或没有明显差异的情况下。

Let's say that the functional way takes 50x the times of regular loop.假设函数方式需要 50 倍于常规循环的时间。 If a regular loop takes 1ms, it means the functional takes 50ms and in most of the case that is still okay.如果常规循环需要 1 毫秒,则意味着函数需要 50 毫秒,并且在大多数情况下仍然可以。

So I wouldn't sacrifice my code for optimization in that case, especially in application and / or shared repo.因此,在这种情况下,我不会牺牲我的代码进行优化,尤其是在应用程序和/或共享存储库中。

However, when I code videogames, I usually try to do regular loop.但是,当我编写视频游戏时,我通常会尝试进行常规循环。 Both for performance reasons, but also because in that context usually you have to deal with arrays of bytes, and I find functional programming less flexible.既是出于性能原因,也是因为在这种情况下,您通常必须处理字节数组,而我发现函数式编程不太灵活。

Said that: in JS the problem of array's method is that they aren't lazy.说:在JS中,数组方法的问题在于它们不懒惰。 It means, in your case, you're iterating twice the array because you call two methods ( filter and map ).这意味着,在您的情况下,您要对数组进行两次迭代,因为您调用了两个方法( filtermap )。 In other languages (eg Rust) such method are "lazy", and they're not invoked until you actually do something with the iterator: that reduce the performance issue you can have compare to a regular loop.在其他语言(例如 Rust)中,这种方法是“懒惰的”,并且在您实际使用迭代器执行某些操作之前不会调用它们:与常规循环相比,这减少了您可能遇到的性能问题。

There are library in JS that supports lazy methods (eg RxJS on observable), so you might want to check those if you're looking for something in the middle (saving a bit of perf while still using a functional approach). JS 中有一些库支持惰性方法(例如 observable 上的 RxJS),因此如果您正在寻找中间的东西(在仍然使用函数式方法的同时节省一点性能),您可能需要检查这些库。

The difference between Array.map and a for-loop is that the for-loop does nothing more than iterating over the values of the array. Array.map和 for 循环之间的区别在于 for 循环只不过是迭代数组的值。 Within the body of the loop, you can do whatever with these values.在循环体中,您可以对这些值执行任何操作。 Array.map does more than that. Array.map作用不止Array.map It iterates over the array, creating a new array with the value of the callback invoked on every value.它遍历数组,创建一个新数组,其中包含对每个值调用的回调值。

In my opinion you should use a for-loop as much as possible over Array.map , while it is a lot quicker.在我看来,您应该在Array.map尽可能多地使用 for 循环,虽然它要快得多。 Use Array.map when you want to create a new array with mutated values in the original array.当您想在原始数组中创建一个具有变异值的新数组时,请使用Array.map

Basically these are the same as:基本上这些是相同的:

For-loop:循环:

const array = [1, 2, 3];
const mutatedArray = [];

for(let i = 0; i < array.length; i++) {
    let mutatedValue = array[i] * 2;

    mutatedArray.push(mutatedValue);
}

Array.map:数组.map:

const array = [1, 2, 3];
const mutatedArray = array.map(x => x * 2);

It is a lot cleaner and quicker to write.写起来更干净、更快捷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM