Coder Social home page Coder Social logo

nicolo-ribaudo / proposal-array-find-from-last Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tc39/proposal-array-find-from-last

0.0 1.0 0.0 131 KB

Proposal for Array.prototype.findLast and Array.prototype.findLastIndex.

Home Page: https://tc39.es/proposal-array-find-from-last/index.html

License: MIT License

HTML 100.00%

proposal-array-find-from-last's Introduction

Proposal for .findLast() and .findLastIndex() methods on array and typed array.

Status

This proposal is a stage 3 proposal and seeking implementation feedback.

Motivation

Finding an element in an array is a very common programming pattern.

The proposal has a major concerns: Semantical. Which means clearly representing the operation i want.

And with the changes. There's a sugar here: Performance. Avoid obvious overhead. And may improve the constant factors in the time complexity. Even there's not an order of magnitude change. But it's may useful in some performance-sensitive scenarios. eg: React render function.


ECMAScript currently supports {Array, %TypedArray%}.prototype.indexOf and {Array, %TypedArray%}.prototype.lastIndexOf to find an index of some value in the array.

There is also {Array, %TypedArray%}.prototype.find and {Array, %TypedArray%}.prototype.findIndex to find an element or its index in the array that satisfies a provided condition.

However, the language does not provide a method to find an element from the last to the first of an array with a condition function.

[...[]].reverse().find() is a workaround but there are two issues:

  1. unnecessary mutation (by reverse).
  2. unnecessary copy (to avoid mutation)

For .findIndex(), you are required to perform additional steps after calling the method (re-calculate the index and handle the -1) to calculate the result of [...arr].reverse().findIndex().

Therefore there is a third issue:

  1. complex index calculation

So, perhaps we need something directly and effectively. In this proposal, they are {Array, %TypedArray%}.prototype.findLast and {Array, %TypedArray%}.prototype.findLastIndex.

Scenarios

  • You know find from last may have better performance (The target element on the tail of the array, could append with push or concat in a queue or stack, eg: recently matched time point in a timeline).
  • You care about the order of the elements (May have duplicate item in the array, eg: last odd in the list of numbers).
  • Etc.

Core features

Add {Array, %TypedArray%}.prototype.findLast and {Array, %TypedArray%}.prototype.findLastIndex.

This would behave the same as Array.prototype.find and Array.prototype.findIndex but would iterate from the last to the first.

eg:

const array = [{ value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }];

array.find(n => n.value % 2 === 1); // { value: 1 }
array.findIndex(n => n.value % 2 === 1); // 0

// ======== Before the proposal =========== 

// find
[...array].reverse().find(n => n.value % 2 === 1); // { value: 3 }

// findIndex
array.length - 1 - [...array].reverse().findIndex(n => n.value % 2 === 1); // 2
array.length - 1 - [...array].reverse().findIndex(n => n.value === 42); // should be -1, but 4

// ======== In the proposal =========== 
// find
array.findLast(n => n.value % 2 === 1); // { value: 3 }

// findIndex
array.findLastIndex(n => n.value % 2 === 1); // 2
array.findLastIndex(n => n.value === 42); // -1

Slides

Polyfill

Related

Proposer

Champions:

  • @Kingwl (Wenlu Wang, KWL)
  • @DanielRosenwasser (Daniel Rosenwasser, DRR)

proposal-array-find-from-last's People

Watchers

 avatar

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.