Академический Документы
Профессиональный Документы
Культура Документы
GUITA R . S CIENCE. COMPUTER STUFF. G A M B I T E E R . S O M E T I M E S I G E T T H I N G S R I G H T.
Home Archives
I’ve measured and tested and what not javascript loops and always came
to the conclusion that we don’t know how Javascript is going to behave.
In the following example function f1 is substantially faster than
function f2 . Can’t understand why.
1 function tic() {
2 the_time = performance.now()
3 }
4
5 function toc() {
6 console.log(performance.now() - the_time)
7 }
8
9 function f1(z) {
10 let len = z;
11 let buff = []
12 for (let i = 0; i < len; i++) {
13 buff[i] = i;
14 }
15 return buff;
16 }
17
18 function f2(z) {
18 function f2(z) {
19 let len = z;
20 let buff = []
21 while (len--) {
22 buff[len] = len;
23 }
24 return buff
25 }
26 tic(); f1(2 << 20); toc();
27 tic(); f2(2 << 20); toc();
Your Results:
f1: 86
f2: 195
Normally I would say that the reversed while would be faster. It has been
in other instances. But in this case we want to create an array with
numbers from 0 to z-1 . In this case the for loop is much faster
(tested in Chrome, Firefox, Opera). What is going on? Is it a caches
problem where JS engines expect the “straight” order and not the
reverse one?
update
1 function f2_new(z) {
2 var len = z;
3 var buff = [];
4 buff.length=len;
5 while (len--) {
6 buff[len] = len;
7 }
8 return buff;
9 }
Notice that I’m creating a buffer with the correct length before looping
over it. If you run this version you’ll get the fastest while loop.
f2_new: 91
This version is now on par with the for loop and in some benchmarks it
is substantially faster. So, order restored in the realm of Javascript. Me
happy again.
Lemon
2018‑05‑19
animation #animation, #pug, #sass, #css3
reading #links
#links
Saturday, 12 May
2018‑05‑12
I Used The Web For A Day With JavaScript Turned Off ‑ Unfortunately the
web is a race between those that push stuff out and those that have to
implement them. (smashingmagazine.com)
Getting Started With The Web MIDI API ‑ It is a real pity that web Midi
isn’t well supported across browsers. (smashingmagazine.com)
Consensus Mechanisms — As Detailed and Concise as possible! ‑ Great
read on such important algorithms, informative, mainly focused on
blockchain technologies. (hackernoon.com)
“A Sloppy Machine, Like Me”: The History of Video Synthesizers
(wearethemutants.com)
What’s the difference between UX and UI design? ‑ Everywhere you need
to tick boxes, put things into small confined spaces, otherwise they’ll
bite you in the hand. (medium.freecodecamp.org)
#links
Help! Need an Index Card and Contacts Manager
Apps for OS X
2018‑02‑01
I’ve rely on physical index cards for many tasks related to managing
information, but I’d like to have the same functionality on the desktop. I
don’t want it integrated in a “brainstorming, mindmapping” package, or
in a “screenplay” software. I wanted something like a shoe box of index
cards, that I could just sort by some criterion, or group under certain
labels. Just something simple. In design making things simple is
sometimes the hardest.
My other need is about the Contacts app in OS X. As all Apple software,
Contacts is suffering from the “we don’t have time to think about OS X
software while we are stealing candy from hipsters buying iPhones”
syndrome. I really need to have some way of categorizing, cleaning, and
searching through my contacts easily. Every time I open Contacts, I’m
surprised by the number of people that I don’t know and never
contacted. I’m starting to think that the app just comes up with fake
contacts like fake twitter followers.
If you know about an app that can help me, please share it with me! I’m
avidly searching for apps to solve these two tasks.
Sorting algorithms are one of those basic concepts that every CS course
has, and that act as a great pedagogic tools for teaching students about
how computers work. But many times they tend to be difficult to
visualize and explain. That’s why it is always great to find some
visualizations that are very clear and that compare several algorithm
side by side.
computer science #sort algorithms, #visualization
Next