npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

optimized.js

v1.0.0

Published

Helpful JS for-loop shorthands

Downloads

1

Readme

Optimized.JS

This package aims to optimize your JavaScript where speed it critical. It has long been known that the JS for-loop is the fastest method of looping through Arrays, but they have always been more of a pain to code. The built-in JavaScript Array prototypes are so user-friendly, but also so slow. It is a mystery how they can be so much slower than a for-loop. This package provides shorthands for the most efficient ways of looping through arrays.

This is part of my larger goal to make a program that fully optimizes your JavaScript.

JavaScript For-Loops

In JavaScript, you can loop through any array with forEach, it is simple and (for most cases) fast enough. The built-in JS Array prototypes are getting faster with every new version, but they are still not as fast as a for-loop. All of the Array functions in this project are as fast as a for-loop.

let array = [1, 2, 3, 4, 5];

// this is the fastest way of looping through an array
for (var i = 0; i < array.length; i++) {
	let elem = array[i];

	/* do something great */
}

// this is much more convenient, but slow
array.forEach((elem) => {
	/* do something great */
});

// I have combined them into this (80% faster than Array.prototype.forEach)
forEach(array, (elem) => {
	/* do something great */
});

Objects

Another downfall of the Array prototypes is that they are for Arrays (as the name might suggest). It is because of this that I have made this library work on both Objects and Arrays in the same container function. I do this mainly because it is monotonous to implement into both the for-loops and the protorypes. All of the array functions in this project also work on objects.

Functions

These functions are made to be easy to use, just like JavaScript's convenient built-ins.

import {forEach, filter, map} = require("optimized.js");

//
// For use with Arrays
//

const arr = [1, 2, 3, 4, 5, 6];

// Array.prototype.forEach alternative (80% faster)
forEach(arr, (elem, i) => {/* do something */});

// Array.prototype.filter alternative (70% faster)
filter(arr, (elem, i) => {/* do something */});

// Array.prototype.map alternative (60% faster)
map(arr, (elem, i) => {/* do something */});

//
// For use with Objects
//

const obj = {a: 1, b: 2, c: 3};

// Array.prototype.forEach alternative (80% faster)
forEach(obj, (elem, i, key) => {/* do something */});

// Array.prototype.filter alternative (70% faster)
filter(obj, (elem, i, key) => {/* do something */});

// Array.prototype.map alternative (60% faster)
map(obj, (elem, i, key) => {/* do something */});

Example

Because these behave the same way as their prototype alternatives, this example is for use with an object.

import {map} = require("optimized.js");

const obj = {a: 1, b: 8, x: 23, y: 1, z: 53};

const greaterThanTen = filter(obj, (elem, i, key) => elem > 10); // {x: 23, z: 53}

const addEightyEight = map(greaterThanTen, (elem, i, key) => elem + 88); // {x: 111, z: 141}

Notes

The team behind JS is constantly improving their built-in functions. There are some cases where the difference between my functions and the prototypes are minimal. My functions perform best when there are lots of items and with non-empty arrays. I made this because I needed it for my game development. I wanted maximum performance, but I was tired of writing for-loops. This is much more elegant. I do plan on adding more functionality and even a code parser/optimizer in the future. If you need this in CommonJS, let me know. Personally, I love CommonJS more. Thanks!