Helpful for-loop shorthands in JavaScript

Overview

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!

You might also like...

This is my first website. It has helpful information, games, lots of pages & more.

This is my first website. It has helpful information, games, lots of pages & more.

Mitko.Vtori World This is my first website. It has helpful information, games, lots of pages & more. 🎞 Presentation & Introduction Here's link to my

Dec 30, 2022

An example implementation of the slack-gpt starter which ingests confluence pages to create a helpful slack bot

Slack-GPT (HR bot example implementation) Table of Contents Introduction Prerequisites Creating and installing the application Configuration Starting

Jul 31, 2023

This is my to-do list website built with html, css and JavaScript. In this project I used Webpack to bundle JavaScript and ES6 modules to write modular JavaScript.

This is my to-do list website built with html, css and JavaScript. In this project I used Webpack to bundle JavaScript and ES6 modules to write modular JavaScript.

To-Do-List App This is my to-do list website built with html, css and JavaScript. In this project I used Webpack to bundle JavaScript and ES6 modules

Sep 20, 2022

Reference for How to Write an Open Source JavaScript Library - https://egghead.io/series/how-to-write-an-open-source-javascript-library

Reference for How to Write an Open Source JavaScript Library The purpose of this document is to serve as a reference for: How to Write an Open Source

Dec 24, 2022

Open Source projects are a project to improve your JavaScript knowledge with JavaScript documentation, design patterns, books, playlists.

Open Source projects are a project to improve your JavaScript knowledge with JavaScript documentation, design patterns, books, playlists.

It is a project I am trying to list the repos that have received thousands of stars on Github and deemed useful by the JavaScript community. It's a gi

Aug 14, 2022

Javascript-testing-practical-approach-2021-course-v3 - Javascript Testing, a Practical Approach (v3)

Javascript-testing-practical-approach-2021-course-v3 - Javascript Testing, a Practical Approach (v3)

Javascript Testing, a Practical Approach Description This is the reference repository with all the contents and the examples of the "Javascript Testin

Nov 14, 2022

Navigation-Menu-Javascript - A simple Navbar navigation using vanilla javascript, to change links to the active link when clicked.

Navigation-Menu-Javascript - A simple Navbar navigation using vanilla javascript, to change links to the active link when clicked.

Navigation-Menu-Javascript A simple Navbar navigation using vanilla javascript, to change links to the active link when clicked. Desktop view Mobile v

Feb 16, 2021

Ping.js is a small and simple Javascript library for the browser to "ping" response times to web servers in Javascript

Ping.js Ping.js is a small and simple Javascript library for the browser to "ping" response times to web servers in Javascript! This is useful for whe

Dec 27, 2022

MenuSlider-Javascript - How to create a menu slider with vanilla javascript

MenuSlider-Javascript How to create a menu slider with vanilla javascript Instal

Feb 8, 2022
Owner
E
I just like to fiddle with code.
E
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
:loop: Create loopy loading animations

Sonic.js See some examples! Create your own with Sonic Creator! Sonic is a tool that you can use to create spinny-loady-thingies on the fly. It's best

James Padolsey 813 Nov 21, 2022
Official Repository of Finite Loop Club website.

Finite Loop Club - Official Repo Built with: Running the Project Clone the repo git clone https://github.com/FiniteLoop-NMAMIT/flc-website Install the

FiniteLoop Club NMAMIT 4 Dec 26, 2022
An interactive Bitcoin tutorial for orange-pilled beginners. Illustrates technical Bitcoin concepts using JavaScript and some Bitcoin Core RPC commands. Programming experience is helpful, but not required.

Try Bitcoin Try Bitcoin is an interactive Bitcoin tutorial inspired by and forked from Try Regex, which is inspired by Try Ruby and Try Haskell. It il

Stacie Waleyko 33 Nov 25, 2022
A list of helpful front-end related questions you can use to interview potential candidates, test yourself or completely ignore.

Front-end Developer Interview Questions This repository contains a number of front-end interview questions that can be used when vetting potential can

H5BP 56.1k Jan 4, 2023
Show a helpful summary of test results in GitHub Actions CI/CD workflow runs

Test Summary Produce an easy-to-read summary of your project's test data as part of your GitHub Actions CI/CD workflow. This helps you understand at-a

Test Summary 293 Jan 2, 2023
VSCode extension with helpful code snippets for SolidJS.

Solid Snippets VSCode extension with helpful code snippets for SolidJS. GET THE EXTENSION Snippets Trigger Content Languages JSX sinput→ Input two-way

SolidJS Community 11 Dec 8, 2022
A community-led experiment to build better docs and helpful content :)

Website This website is built using Docusaurus 2, a modern static website generator. Installation $ npm Local Development $ npm start This command s

Battlesnake Official 9 Jan 1, 2023
Statistics plugin for RemNote that will give you some helpful numbers, charts and heatmap for your knowledge base.

RemNote statistics plugin Features This plugin will give you the following statistics: Retention rate Number of cards due in future Type of buttons yo

Henrik 3 Sep 9, 2022