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

the-context-js

v1.0.9

Published

This project demonstrates a simple context management system using JavaScript. It allows you to create contexts, bind values to keys within those contexts, and retrieve those values within a handler function.

Downloads

145

Readme

Context Manager

This project demonstrates a simple context management system using JavaScript. It allows you to create contexts, bind values to keys within those contexts, and retrieve those values within a handler function.

Overview

The main components of this example are:

  • Context Creation: You can create a new context using Context.createContext().
  • Binding Values: Values can be bound to keys in the context using the bind method.
  • Retrieving Values: Values can be retrieved using the get method within a handler function that has access to the context.
  • Context Isolation: The context is isolated to the handler, meaning that accessing the context outside of the handler will return undefined.

How to Use

  1. Import the Context Module:

    import Context from 'the-context-js';
    // OR ||
    const { default: Context } = require("the-context-js");
  2. Create a Context: Use Context.createContext() to create a new context instance.

    const theContext = Context.createContext();
  3. Bind Values: Use the bind method to associate keys with values in the context.

    theContext.bind("key1", "value1");
    theContext.bind("key2", "value2");
  4. Define a Handler: Create a function that retrieves values from the context.

    const theHandler = function() {
    const theContext = Context.getContext();
    const key2 = theContext.get("key2");
    console.log(key2); // Outputs: value2
    };
  5. Create a New Handler with Context: Use containerWithContext to create a new handler that has access to the context.

    const newHandler = theContext.containerWithContext(theHandler);
    newHandler(); // Call the handler with context
  6. Call the Handler Without Context: If you call the handler directly without context, it will not have access to the bound values.

    theHandler(); // Outputs: undefined
  7. Global Context Access: Attempting to access the context globally will return undefined.

    const anotherContext = Context.getContext(); // This will be undefined
    console.log('GLOBAL: anotherContext', anotherContext); // Outputs: undefined

Benchmark

  • Run a benchmark: node --expose-gc benchmark.js
Last Reports:
Context.createContext: 1.625 ms, 1000 iterations, 615498.25 ops, Total Memory: 42544.00 KB
Context.getContext: 0.581 ms, 1000 iterations, 1720282.13 ops, Total Memory: 43412.88 KB
Context.bind: 0.845 ms, 1000 iterations, 1183431.95 ops, Total Memory: 43998.81 KB
Context.get: 0.253 ms, 1000 iterations, 3955696.20 ops, Total Memory: 44259.15 KB
Context.unbind: 0.342 ms, 1000 iterations, 2924831.82 ops, Total Memory: 44528.48 KB

Debugging

  • Monitor Contexts: node debug.js
Commands:

1. create sampleContext --> ID: 18
2. bind key value 18
3. get all
4. get 18