Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance implications of using Functional style Javascript vs "procedural"

Has anybody done benchmarking, or can link to an article on this subject? Particularly interested in IE results, since usually JS performance is not a problem in other browsers.

I would like to know how much slower it is to do something like:

var numbers = [1, 2, 3, 4, 5, 6, 7];
var results = numbers.map(function() { 
  // do some stuff
});

instead of the typical:

var numbers = [1, 2, 3, 4, 5, 6, 7];
var results = [];

for (var i = 0; i < numbers.length; i++) {
  var number = numbers[i];
  var result;
  // do some stuff
  results.push(result);
}

I obviously prefer the functional style, but I assume the extra overhead of calling an extra function for each item could slow things down with big collections.

Thanks!

like image 303
adamJLev Avatar asked Aug 09 '10 15:08

adamJLev


People also ask

Why is functional programming better in JavaScript?

Functional programming is a really powerful and beautiful paradigm to write better code. It introduces a lot of benefits, such as bug-free applications, efficiency, and making code easier to test, reuse, etc. It not only improves your code but also improves how you code.

Is JavaScript functional or procedural?

JavaScript can function as both a procedural and an object oriented language. Objects are created programmatically in JavaScript, by attaching methods and properties to otherwise empty objects at run time, as opposed to the syntactic class definitions common in compiled languages like C++ and Java.

Is JavaScript more functional or object-oriented?

To be more precise, JavaScript is a prototype based Object Oriented Language, which means it doesn't have classes, rather it defines behaviors using a constructor function and then reuse it using the prototype. Note: Even the classes provided by ECMA2015 are objects.

What are the features of JavaScript that support functional programming?

JavaScript has the most important features needed for functional programming: First class functions: The ability to use functions as data values: pass functions as arguments, return functions, and assign functions to variables and object properties.


1 Answers

Not content with the lack of proof on this subject, I wrote a short benchmark. It's far from perfect but I think it answers the question.

I ran it in IE 8/win, and while the functional method is slower, it's never going to be the bottleneck in real code. (Unless you're doing stuff that you shouldn't be doing in the client anyway)

So I will be using the cleaner approach whenever I have to pick (yay)

(Best of 5)
Functional method: 453ms
Old school approach: 156ms

Array.prototype.map = function(fun) {
  var len = this.length >>> 0;
  if (typeof fun != "function")
    throw new TypeError();

  var res = new Array(len);
  var thisp = arguments[1];
  for (var i = 0; i < len; i++) {
    if (i in this)
      res[i] = fun.call(thisp, this[i], i, this);
  }

  return res;
};

/**
 *
 *
 */

// Initialize test array
var numbers = [];
for (var i = 0; i < 100000; i++) numbers.push(i);

// Benchmark!
var start = +new Date();

// Test 1
var results1 = numbers.map(function(num) {
  return num + num;
});

alert('1. Functional map:' + (start - new Date()));
start = +new Date();

// Test 2
var results2 = [];
for (var j = 0, l = numbers.length; j < l; j++) {
  var num = numbers[j];
  results2.push(num + num)
}

alert('1. Old school approach' + (start - new Date()));
start = +new Date();
like image 152
adamJLev Avatar answered Nov 14 '22 16:11

adamJLev