Coder Social home page Coder Social logo

sachasi / last_item Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ultirequiem/last_item

0.0 0.0 0.0 31 KB

⚡ Get the last N items of an array or just the last one, fast!

Home Page: https://ulti.js.org/last_item

License: MIT License

TypeScript 100.00%

last_item's Introduction

Last Item

Code Coverage Deno Doc

Get the last N items of an array or just the last one, faster than Array.prototype.slice/Array.prototype.at.

Usage

The API is the same on all this platforms ✔️

import { lastItem } from "https://deno.land/x/last_item/mod.ts";

const numbers = [1, 2, 3, 4, 5];

lastItem(numbers); //=> 5

lastItem(numbers, 3); //=> [3, 4 , 5]
import { lastItem } from "@ultirequiem/last-item";

You can use any CDN 🔥

Eg 👉 ESM ↔️ SkyPack 🆚 Script Tag ↔️ JSDelivr

Alternatives

This is a utility module, which focuses on being faster than its native counterparts and similar modules.

In the vast majority of cases you should prefer Array.prototype.slice or Array.prototype.at if performance is not an issue.

One of the biggest advantages of this module is also that it is packaged to be easy to use in various environments such as Node.js, Deno or the Browser.

Get Last Array Item

const abc = ["a", "b", "c"];

lastItem(abc); //=> "c"

abc.at(-1); //=> "c"

Get N Last Array Items

const animals = ["Tiger", "Horse", "Lion"];

lastItem(animals, 2); //=> ["Horse", "Lion"]

animals.splice(-2); //=> ["Horse", "Lion"]

Similar Modules

This module is also quite fast, so I am including it as an alternative, I must mention that it has some bugs and open pull requests for several years and there seems to be no activity.

The biggest drawback of this module in my opinion is the poor error handling and it's strange behavior, here are some things I noticed.

This module would throw a RangeError

This module would throw a TypeError

last(["a", "b", "c"], 4); //=> [ undefined, 'a', 'b', 'c' ]

This module would throw a RangeError

This module would throw a RangeError

Benchmarks

Check the benchmark code on bench.ts

Using Deno's built-in benchmark runner

benchmark       time (avg)             (min … max)       p75       p99      p995
-------------------------------------------------- -----------------------------
Slice        44.75 ns/iter  (39.73 ns … 801.56 ns)  42.69 ns  79.87 ns  80.45 ns
At           81.23 ns/iter  (79.83 ns … 104.44 ns)  81.35 ns  94.27 ns  94.68 ns
Array Last   13.93 ns/iter   (13.65 ns … 29.26 ns)   13.7 ns  15.01 ns  15.38 ns
Last Item    14.21 ns/iter    (12.7 ns … 19.77 ns)  14.87 ns  15.37 ns  15.51 ns

Version 0.1.2

Benchmarks are run on GitHub Actions on each commit, you can see the latest runs here.

Documentation

Is hosted on Deno Doc 📄

Support

Open an Issue, I will check it a soon as possible 👀

If you want to hurry me up a bit send me a tweet 😆

Consider supporting me on Patreon if you like my work 🙏

Don't forget to start the repo ⭐

Authors

Eliaz Bobadilla - Creator and Maintainer 💪

See also the full list of contributors who participated in this project ✨

Versioning

We use Semantic Versioning. For the versions available, see the tags 🏷️

Licence

Licensed under the MIT License 📄

last_item's People

Contributors

ultirequiem avatar web-flow 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.