7,416 karma · joined Oct 23, 2009 (17y ago) · 1,582 submissions
Email: charles@codemix.com
comment 12y ago · link · parent
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
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
comment 12y ago · link · parent
comment 12y ago · link · parent
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
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
These fall outside of the common uses of these methods, and most people don't even know they exist.
comment 12y ago · link · parent
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
comment 12y ago · link · parent
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
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.