dg92 / performance-analysis-js Goto Github PK
View Code? Open in Web Editor NEWMap/Reduce/Filter/Find Vs For loop Vs For each Vs Lodash vs Ramda
Map/Reduce/Filter/Find Vs For loop Vs For each Vs Lodash vs Ramda
Can the for-of loop end result be added to this comparison table in the readme?
You could optimize the for loop by hoisting your iterator and posts.length. It would take much less time if you didn't have to check posts.length every time.
for(let i=0; i<posts.length; i++) {
posts[i].upvotes = (posts[i].upvotes +posts[i].commentCount)/divider;
}
Would take less time implemented this way:
var i;
const len = posts.length;
for(i=0; i<len; ++i) {
avg += (posts[i].upvotes +posts[i].downvotes +posts[i].commentCount)/3;
}
This is more of a question than an issue. I'm noticing that there are + signs than I'm used to, and I'm wondering what the purpose is. For example, on the reduce section, the equation looks like
(+posts[i].upvotes + +posts[i].downvotes + +posts[i].commentCount)
with + symbols before each element and then additional ones in between. I've tested with
(posts[i].upvotes + posts[i].downvotes + posts[i].commentCount)
and it seems to work. Is there something that the additional symbols are doing?
You are not taking in consideration time needed to work with memory, but you have to in such tests, where main work depends exactly on construction of big arrays and freeing them before next test.
Simple example.
In mapPerformance(posts) execution time of "forEach" depends on running order,
just move "forEach" block before "for loop", and you will see it starts to run "faster" than "for loop".
I have changed order and added 2 copies of tests, several runs:
posts.length: 100000
*************** Map performance check ***************
js map: 76.214ms
forEach1: 18.749ms
for loop1: 41.964ms
for loop2: 25.268ms
forEach2: 44.676ms
lodash map: 39.380ms
*************** Map performance check ***************
js map: 54.923ms
forEach1: 22.339ms
for loop1: 40.497ms
for loop2: 25.301ms
forEach2: 36.331ms
lodash map: 46.882ms
*************** Map performance check ***************
js map: 52.755ms
forEach1: 13.488ms
for loop1: 36.412ms
for loop2: 18.201ms
forEach2: 32.484ms
lodash map: 29.668ms
*************** Map performance check ***************
js map: 58.978ms
forEach1: 18.157ms
for loop1: 42.817ms
for loop2: 25.558ms
forEach2: 40.048ms
lodash map: 37.493ms
What we see? What conclusion?
What can we say with confidence?
Just that forEach and for loop have no difference and you can choose what you want.
PS:
node v10.16.1
The results are practically invalid since the tests don't actually care about the results. This means that javascript engines can and will optimise away critical code.
*************** Map performance check ***************
js map: 15.636ms
for loop: 8.461ms
for each: 17.376ms
lodash map: 18.545ms
*************** Map performance check with result check ***************
js map: 6.106ms
sum: 49886314
for loop: 22.985ms
sum: 49886314
for each: 7.250ms
sum: 49886314
lodash map: 27.305ms
sum: 49886314
The latter case is where I just console.log the sum of commentCounts after each run and outside the timing blocks. This completely turns the results around.
Hey const {
map,
reduce,
filter,
find,
random
} = require('lodash');
Is this syntax pulling specific methods from a library ?? ( map, reduce, filter, find and random) are pulled from lodash here right ?
These perf numbers are invalid as you're NOT measuring the same thing - for example, according to the docs:
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
The for loop code doesn't do the same thing as map would:
console.time('for loop');
for(let i=0; i<posts.length; i++) {
posts[i].upvotes = (+posts[i].upvotes + +posts[i].commentCount)/divider;
}
console.timeEnd('for loop');
Sure, you can say that you're using all these functions just to iterate over an array, but that would clearly be a misuse of the APIs
find()
has other issues - your for loop implementation returns the last element as supposed to the first one from Array.find.
When doing perf analysis it is really important for the code alternative to do the same thing, otherwise the comparison is pointless
Just a couple thoughts to improve the tests if you're still working on this.
I might pitch in and try my hand at one or all of these. Regardless of the results, it's an interesting idea you had to think to test these things.
The for loop - find test is significantly slower because it should break when the object is found, since it wants the first occurrence in the array
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.