GithubHelp home page GithubHelp logo

janhancic / fasterforeach.shim Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 152 KB

A hack to see if I can make a faster forEach shim.

Home Page: http://hancic.info/creating-a-faster-foreach-shim

License: MIT License

JavaScript 100.00%

fasterforeach.shim's Introduction

fasterForEach

A hack to see if I can make a faster forEach shim for IE8.

About

This is a hack that adds support for Array#forEach to IE8 but in a different way than the ES5 shim.

This basically rewrites the item processor (callback you pass into forEach) into a regular for loop and executes that (as oposed to executing the callback on each iteration). The result is that instead of 1 + n function invocations that you get with native or shimed forEach you get only two function invocations.

I created two nearly identical versions, the only difference is how the rewriten code gets executed. One uses eval and the other uses new Function().

Testing shows that both versions are faster than the ES5 shim. However I only have access to a VM from modern.ie running on a Macbook Air. But please visit this jsPerf test page and give it a spin so I can collect more data.

Is it usable?

I haven't tested this in any real project, but in theory I don't see why it couldn't be used.

License

Licensed under MIT. See LICENSE.md file for details.

fasterforeach.shim's People

Contributors

janhancic avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

fasterforeach.shim's Issues

Context in eval version

In eval version, the item processor has access to variables defined inside fasterForEachEval.

Can be fixed with eval.call(window, .... Unfortunately, this needs to create a temporary function on the window object which needs to be removed after.

Function params

Ierator functions do not allways take all the arguments and the might be named differently too.

Line 19: the replace "function ... {" should be a regexp
Or maybe the first occurence of the "{".

Also if I give in a function that is

myArray.forEach(function (model) { ... }),; // maintains a collection of models

this will be stringified to "function (model) { ... }" so the variables "item", "idx", "array" defined in the string will not work.

I think it would be beneficial if the provided iterator method would be parsed before any other operation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.