Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed Arrays #90

Closed
letmaik opened this issue Jul 8, 2015 · 6 comments
Closed

Typed Arrays #90

letmaik opened this issue Jul 8, 2015 · 6 comments
Labels

Comments

@letmaik
Copy link

letmaik commented Jul 8, 2015

I wanted to use the slice function of TypedArray and saw there is no polyfill. I also couldn't find one somewhere else with a quick search. Is it possible to implement that? I'm not sure as it seems to be quite native and hard to emulate without actually making a copy of the array. But maybe (hopefully) I'm wrong.

@letmaik
Copy link
Author

letmaik commented Jul 8, 2015

Ah wait, it does actually make a copy. They say "shallow" copy, but typed arrays are of primitive types anyway so I don't really understand this distinction. Possibly for future types which can host references to more structured things. So, a polyfill should be easy then, basically a for loop copying elements into a new typed array of the same type and returning it.

EDIT: found a polyfill at https://github.com/inexorabletash/polyfill/blob/master/typedarray.js#L795 though this emulates all of typedarrays I think

@zloirock
Copy link
Owner

zloirock commented Jul 8, 2015

Currently, core-js not adds typed arrays polyfill. It's possible, but slow - getters / setters for each element in modern environment (but most missing, IIRC, only in IE9) and not complete in ES3 environment. You can see this polyfill.

I'm thinking about adding only missing methods or, possible, adding complete polyfill, but not in near versions.

@zloirock zloirock changed the title TypedArray.prototype.slice() polyfill Typed Arrays Jul 8, 2015
@zloirock zloirock added the es6 label Jul 8, 2015
@letmaik
Copy link
Author

letmaik commented Jul 8, 2015

Given the fast advances of browsers I wouldn't waste time adding full typed arrays polyfills, but rather only add the missing functions of the latest browser versions. Like the slice method in my case. I see core-js more as a tool to be able to use the future now, not as something to support old browsers, but of course, there is some overlap.

@letmaik
Copy link
Author

letmaik commented Jul 8, 2015

For reference, this is my working polyfill, adapted from https://github.com/inexorabletash/polyfill/blob/master/typedarray.js:

"use strict"

for (let TA of [Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array]) {
  if (!TA.prototype.slice) {
    TA.prototype.slice = function (start, end) {
      var len = this.length
      var relativeStart = start
      var k = (relativeStart < 0) ? max(len + relativeStart, 0) : Math.min(relativeStart, len)
      var relativeEnd = (end === undefined) ? len : end
      var final = (relativeEnd < 0) ? max(len + relativeEnd, 0) : Math.min(relativeEnd, len)
      var count = final - k
      var c = this.constructor
      var a = new c(count)
      var n = 0
      while (k < final) {
        var kValue = this[k]
        a[n] = kValue
        ++k
        ++n
      }
      return a
    }
  }
}

Note that I couldn't use Object.getPrototypeOf(Int8Array.prototype) to get to the %TypedArray% constructor as described here. Chrome just gave me back Object. So I worked around that by explicitly adding the function to each type.

@zloirock zloirock mentioned this issue Aug 16, 2015
@zloirock zloirock mentioned this issue Nov 24, 2015
20 tasks
@zloirock
Copy link
Owner

Available in 2.0.

@letmaik
Copy link
Author

letmaik commented Dec 24, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants