Comments (9)
@millermedeiros I forked Underscore.js, creating Lo-Dash, and avoid native ES5 array methods. This gave Lo-Dash performance benefits for older browsers because the fallback didn't have to conform to spec (I don't support sparse arrays). It also allows Lo-Dash to work as a utility lib for code that is added to pages you don't control (widgets) because it won't break when loaded into pages that have something like an older Prototype.js adding bad native shims.
Another benefit of not supporting sparse arrays is it keeps the behavior consistent cross browser as IE < 9 will consider var a = [1, undefined, 3]
as sparse, unlike other browsers.
from amd-utils.
In generic libraries, I generally avoid calling methods that might change when syntax is available. If the syntax version fits the needs then using it means I know it won't change like when someone redefines a method. That is good for the integrity of the intended functionality of the library. It is bad for the flexibility that a dynamic language allows.
I do use arr.push(el)
rather than arr[arr.length] = el
and so I'm not actually consistent with my approach.
from amd-utils.
@millermedeiros Side note that your
every
native fallback is iterating in reverse ordermap
native fallback is incorrect as it won't set the correct length of a sparse array likeArray(3)
indexOf
andlastIndexOf
native fallbacks don't support sparse arrays eitherreduce
andreduceRight
native fallbacks should allow an explicit initial value ofundefined
to be passed
from amd-utils.
@millermedeiros For the ES5 native methods I do use I have a handy weak inference to detect if they have been shimmed, and if so, use my internal alternatives instead.
var reNative = RegExp('^' +
(Object.prototype.valueOf + '')
.replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&')
.replace(/valueOf|for [^\]]+/g, '.+?') + '$'
);
// usage
if (reNative.test(Function.bind)) {
/* do something */
}
from amd-utils.
@jdalton thanks for the comments and for spotting the bugs, fixed all of them on the this commit and also avoided using the natives altogether. Will wait a few days to merge just in case someone else has anything to say.
@petermichaux I will check other places on my code where I fallback to natives (like object/keys ), maybe I will drop them all. thx for the feedback.
from amd-utils.
@jdalton BTW, I normalized the behavior of var a = [1, undefined, 3]
between the browsers by using i in arr
to check for sparse items, see spec-forEach
All tests are passing in all browsers that I tried:
- IE: 6, 7, 8, 9
- FF: 3.6.15 (win), 4.0 (win), 14.0 (mac)
- Chrome: 20 (mac & win), 22.0.1218.0 (canary/mac)
- Opera: 12.00 (mac)
But of course unit tests might not be covering all edge cases (like the ones you spotted and that I already fixed). I improved the test cases today to cover more edge cases and reinforce the behavior.
from amd-utils.
'i in arr' won't normalize as in other browsers the '1 in arr' will be true while in IE 8 and lower it will be false.
from amd-utils.
Careful to contruct proper tests as testing libs may not cover these edge cases in their API either.
from amd-utils.
[edit] this comment contained wrong info!! I was looking at the wrong spec! Updated with correct info:
behavior of i in arr
:
var a = [1, undefined, 3];
// FAIL. sparse only on IE 6-8var b = [1, , 3];
// worksvar c = []; c[3] = undefined;
// worksvar d = new Array(5); d[1] = undefined;
// works
You are right, i in arr
seems to work fine in all the cases but var a = [1, undefined, 3]
and I don't think it's possible to normalize the behavior (damn IE). Now I think I will need to drop support for sparse arrays as well to make it consistent... creating an array like [1, undefined, 3]
isn't that uncommon, I thought the issue only existed with [1, ,3]
(missing item) but that isn't the case.
Thanks once again for the feedback.
from amd-utils.
Related Issues (20)
- use npm prepublish script instead of install
- some users need the AMD modules even if using npm install HOT 3
- add math/round - similar to math/snap but round value instead of floor
- rename math/snap to math/floor?
- lang/isNaN doesn't catch `new Number(NaN)`
- Create tag 0.9.0 HOT 1
- Add deepFilIIn HOT 3
- Ability to test functions directly in the documentation HOT 2
- integrate travis-ci
- make unit tests work on node.js
- integrate some kind of code coverage test
- rename project? HOT 11
- split into multiple projects/packages? HOT 8
- Remove node_modules from git HOT 7
- deprecate financial package? HOT 1
- clone function does not behave well on instances HOT 6
- support exiting iterations early if callback return `false`
- Function currying HOT 4
- Defer to native methods HOT 1
- Better randomization, especially for GUID generation HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from amd-utils.