Better HN
topnewbestaskshowjobs
Log inSign up

phpnode

7,416 karma · joined Oct 23, 2009 (17y ago) · 1,582 submissions

Founder of https://codemix.com/ - authoritative product context which makes sure coding agents build the right thing and humans stay in control.

Email: charles@codemix.com

Submissions

comment 12y ago · link · parent

the other benefit is that `void 0` cannot be overwritten in sloppy mode, whereas you can do something crazy like this:

    undefined = 'lol';

comment 12y ago · link · parent

The issue with coffeescript is that it uses the opposite syntax to JS - `in` for arrays instead of objects. ES6 introduces the `of` keyword for real iterators so i don't really want to use that for either of them. It's tricky. There are other options but they're verbose

    every element as key, value from arr {
      console.log(key, value);
    }

    every property as key, value from obj {
      console.log(key, value);
    }

comment 12y ago · link · parent

The article is "optimisation killers", you're right about caching the array length lookup check being faster, but it doesn't kill V8's ability to optimise functions like the rest of the examples do

comment 12y ago · link · parent

`for ... in` is a relatively slow construct anyway, the faster option (a lot more verbose) is:

    var keys = Object.keys(obj),
        length = keys.length,
        key, i;
    for (i = 0; i < length; i++) {
      key = keys[i];
    }
While this is not exactly the same as for..in, it usually behaves how you'd expect and is significantly faster for a couple of reasons:

1. In a for..in loop the engine must keep track of the keys already iterated over, whereas in the fast version we can simply increment a counter and do a fast array lookup.

2. It's possible to add properties to the object that you're iterating within the body of the for..in statement, and these will be iterated too. Doing such a thing is obviously very rare but it's the kind of edge case the JS engine must keep track of.

comment 12y ago · link · parent

the only issue is i'd like to differentiate between iterating arrays and objects at the syntax level. Coffeescript uses `in` for arrays and `of` for objects, but I agree that becomes confusing. Open to other suggestions!

comment 12y ago · link · parent

it's not that kind of edge case. It simply means that some very uncommon constructs are not supported. If you never use those constructs, which basically nobody does, then it will never fail. (and in fact the failure mode is pretty soft and unlikely to crash your application). If your code worked with this once, it will always work.

comment 12y ago · link · parent

sparse arrays are just one edge case that the native implementation must consider, see this comment - https://github.com/angular/angular.js/issues/6697#issuecomme...

comment 12y ago · link · parent

Thanks again for this, this evening I'll investigate why lodash beats fast.js on some of the iteration functions (although they're close)

comment 12y ago · link · parent

thanks for this! it looks like there's some room for improvement in the .map / .forEach implementations but the rest seems to stack up pretty well.

comment 12y ago · link · parent

no, but I could probably have worded it better. What I mean to say is that if you do this in JS:

   function add (a, b) {
     return a + b;
   }
and this in C:

  int add(int a,int b)
  {       
     return a + b;
  }
you're going to get essentially the same performance.

The latter sentence you quoted refers to the specific builtin functions that fast.js re-implements. So we first get close to native performance in JS, then we beat the builtin functions by doing less work overall.

comment 12y ago · link · parent

regarding your edit, you're exactly right, of course a for loop will be faster. Sometimes you really do need a function call though, in which case fast forEach and map implementations become more useful.

The next step for fast.js are some sweet.js macros which will make writing for loops a bit nicer, because it's pretty painful to write this every time you want to iterate over an object:

    var keys = Object.keys(obj),
        length = keys.length,
        key, i;
    for (i = 0; i < length; i++) {
      key = keys[i];
      // ...
    }
I'd rather write:

   every key of obj {
     // ...
   }
and have that expanded at compile time.

Additionally there are some cases where you must use inline for loops (such as when slicing arguments objects, see https://github.com/petkaantonov/bluebird/wiki/Optimization-k...) and a function call is not possible. These can also be addressed with sweet.js macros.

comment 12y ago · link · parent

if you've ever written a for loop like this, you'd see the exact same issue.

    for (i = 0, total = arr.length; i < total; i++) {
      item = arr[i];
    }
This will also iterate over gaps in the array. Is this a bug?

comment 12y ago · link · parent

This is actually unlikely to be a problem. The edge cases ignored here are actually the same as those ignored by underscore.js, which is obviously a very popular library.

comment 12y ago · link · parent

you're right in that it's important to consider inputs of different sizes. I did this for the indexOf() functions, I'll do the same here, thanks!

comment 12y ago · link · parent

this library does not overwrite any native functions or manipulate any prototypes, you have to call the fast.js versions explictly - it's opt in.

comment 12y ago · link · parent

An alternative interpretation might be - JavaScript, the high level language language that's so awesome that it can out perform native code*

* as long as you make it do less work

comment 12y ago · link · parent

As well as the example of "sparse arrays" shown in the README, there are some additional slight gotchas around `.bind()`, e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

These fall outside of the common uses of these methods, and most people don't even know they exist.

comment 12y ago · link · parent

author of the library here. The native implementations have to follow the spec exactly, which means that they have to guard against specific edge cases. The reimplementations in fast.js do not follow the spec exactly, they optimise for 99.9% of use cases and totally ignore the 0.1%. This, combined with the fact that the JS is JITed straight into machine code anyway gives fast.js a significant advantage - it does less work and so is faster.

1069.

Show HN: Fast.js – faster reimplementations of native JavaScript functions(github.com ↗)

323 points · by phpnode · 12y ago · 162 comments

1070.

Show HN: HTMLing – Polymer/HTML5 compatible templates for Node.js(github.com ↗)

7 points · by phpnode · 12y ago · discuss

comment 12y ago · link · parent

I think he meant that he had no affiliation himself

comment 12y ago · link · parent

This is a religious matter and as such no tactics are off limits. Tea first is wrong.

comment 12y ago · link · parent

Yes, I read it - I was rather hoping no one else would, it seemed just about plausible enough to be convincing.

comment 12y ago · link · parent

The milk in a latte is typically gradually heated to its temperature, whereas when cold milk is added to hot tea, it changes temperature rapidly. This causes the Maillard Reaction, fundamentally changing its flavour. This is like the difference between cooking a steak at immediately high temperature vs gradually increasing temperature. Same resulting temperature but one is delicious and the other is brown and floppy.

0. http://en.wikipedia.org/wiki/Maillard_reaction

comment 12y ago · link · parent

it's a shame for such a distinguished person to make such an amateur mistake - point 10 is indeed answerable:

1. people who put the milk in first have become adept at adding exactly the right amount of milk to a cup of any size. It's a ubiquitous skill, learnt virtually as soon as the person begins drinking tea in this (proper) way. Orwell was clearly inexperienced.

2. adding the milk last requires simultaneous stirring to correctly assess the tea's consistency. This wasteful and complicated activity is not required when the milk is added first, as the milk is evenly mixed with the tea by the pouring process itself. This saves considerable time.

3. adding the milk to an already boiling cup of tea burns the milk, ruining the flavour of the tea.

However, a true tea lover would never soil their tea by adding milk in the first place.

comment 12y ago · link · parent

These machines likely contain very small inventories, the value of their contents is almost certainly exceeded by a typical ATM.

comment 12y ago · link · parent

See also

http://jacquesmattheij.com/Due+Diligence+survival+guide

and

http://jacquesmattheij.com/Due+Diligence+survival+guide-part...

comment 12y ago · link · parent

TDD is not dead, but hopefully the practise of foisting rigid, prescriptive, quasi-religious software development methodologies on creative individuals is.

TDD, like all of these things - eXtreme Programming, agile, has good points and bad. Listen to what these people have to say, and then take what works best for you.

To me, TDD is about writing my code and my tests at the same time, not test first. As soon as I write just enough code to do something, I'll write a test to see if it actually works. I'll then expand on that test and perhaps add a few more as I refactor that piece of code.

This means I still get the benefits of exploratory programming, unconstrained by test first, but I also get the security of knowing that my changes didn't break anything. It feels very natural to develop this way, as the code base grows, your confidence in its quality grows with it, rather than the inverse (lots of passing tests). Also a fast test suite which runs on file save is addictive, I cannot imagine returning to F5 Driven Development now.

1
353637
53