for Vs frameworks each

D

DoomedLung

Ok, I've been having this debate recently regarding performance,
basically I prefer to use good ol' vanilla JS loops ie. for/for in
then the JS frameworks for each eqiv. because further investigation
reveals a hit in performance every time the the each loop runs due to
the closure it creates. So I guess my question is which one is best to
use?

The Doomedlung
 
T

Thomas 'PointedEars' Lahn

DoomedLung said:
Ok, I've been having this debate recently regarding performance,
basically I prefer to use good ol' vanilla JS loops ie. for/for in
then the JS frameworks for each eqiv. because further investigation
reveals a hit in performance every time the the each loop runs due to
the closure it creates.

The closure is created once when defining the function, so that is not the
problem with forEach() (and equivalents); the repeated calls of the callback
are. It is not by coincidence that an optimization step of some compilers
is inlining function calls, but I do not know if this applies to a compiler
of any ECMAScript implementation.
So I guess my question is which one is best to use?

Depends.

<http://www.catb.org/~esr/faqs/smart-questions.html>


PointedEars
 
D

DoomedLung

The closure is created once when defining the function, so that is not the
problem with forEach() (and equivalents); the repeated calls of the callback
are.  It is not by coincidence that an optimization step of some compilers
is inlining function calls, but I do not know if this applies to a compiler
of any ECMAScript implementation.


Depends.

<http://www.catb.org/~esr/faqs/smart-questions.html>

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

Depends on what?
 
D

Dmitry A. Soshnikov

Ok, I've been having this debate recently regarding performance,
basically I prefer to use good ol' vanilla JS loops ie. for/for in
then the JS frameworks for each eqiv. because further investigation
reveals a hit in performance every time the the each loop runs due to
the closure it creates. So I guess my question is which one is best to
use?

The main goal of using the functional style is in increasing of
abstraction and allowing to use polymorphic code blocks on each
element of a collection. E.g. once you've implemented a common
iteration over the collection applying passed function, you can
provide almost unlimited conditions of testing/sorting/searching and
so on actions on elements of the collection.

For example:

var array = [1, 2, 3];

array.map(function (item) {
return item % 2 != 0;
}); [1, 3]

Another function passed functional argument will cause another result.
Inside these methods there is casual loop iteration, but see how the
abstraction is increase. Try e.g. to implement unlimited search
condition for a collection/object. In first case you need to find by
name, in the other -- by name and surname, in the next one -- by name
and by special condition from the outside. For that you can implement
single iteration over the collection and encapsulate inside a method/
function. Then all is needed -- to pass a function which will be
applied to every element/property of that collection. And exactly
function -- because it's the most elegant way to organize unlimited
test/map/filter and so on conditions -- try in contrast to pass that
search conditions via simple object and iterate over the collection
and then on each iteration over that object -- that's just ugly. But
having a functional argument as a test condition -- it is simple and
elegant:

users.find(function (user) {
return user.name == "David"
});

users.find(function (user) {
return user.name == "David" && user.surname == "Surname";
});

users.find(function (user) {
return user.name == "David" && isActive(user);
});

And `.find` can look like:

users.find = function (funArg) {
for (var user in this.data) { // if (..has own ..)
if (funArg(this.data[user])) {
return this.data[user];
}
}
return null;
};

You can enhance this and provide additional argument e.g. `needAll` to
search all users satisfying the search condition.

Iterating every time via loop is a "copy-paste" code reuse.
Encapsulating this loop in a function a passing conditions via object
is a better code reuse, but limited and ugly search condition
implementation. So that's why the functional programming operates with
functions as arguments in this case.

Dmitry.
 
D

Dmitry A. Soshnikov

On Mar 30, 5:55 pm, "Dmitry A. Soshnikov" <[email protected]>
wrote:

[...]
Iterating every time via loop is a "copy-paste" code reuse.
Encapsulating this loop in a function a passing conditions via object
is a better code reuse, but limited and ugly search condition
implementation. So that's why the functional programming operates with
functions as arguments in this case.

Addition: of course if you don't need some polymorphic unlimited test
conditions/applies, you can simply use for-iteration in place - this
is faster and absolutely normal as it is also a construction of the
language. So if some will tell you that using a functional style - is
cool just because it is modern (or sort of), don't listen. But the
other talk is (as I said above) an increasing of abstraction and
providing elegant solutions with a good code reuse - in this case the
functional approach is a very good decision.

Dmitry.
 
A

Antony Scriven

[... on forEach() and friends vs. for-loop ...]

Addition: of course if you don't need some polymorphic
unlimited test conditions/applies, you can simply use
for-iteration in place - this is faster and absolutely
normal as it is also a construction of the language. So
if some will tell you that using a functional style - is
cool just because it is modern (or sort of), don't
listen. But the other talk is (as I said above) an
increasing of abstraction and providing elegant solutions
with a good code reuse - in this case the functional
approach is a very good decision.

Agreed. I'd like to add that in my experience that I get
code right first time more often when using the functional
style; I guess that's because of the reasons you mention.
I think the functional style expresses intent, rather than
the mechanics of the iteration; I certainly don't make any
off-by-one errors when using forEach(). Also you get a new
scope within the function passed to forEach() which can be
helpful. --Antony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top