A high-performance, lightweight JavaScript library for efficient cookie management in the browser. Optimized for speed and memory usage.
- 🚀 High Performance: Uses efficient caching and parsing strategies
- 📦 Lightweight: Zero dependencies, minimal bundle size
- 🔒 Secure: Built-in validation and security features
- 🛠 Developer Friendly: Comprehensive error handling and debugging
- 🌐 Cross-Browser: Works in all modern browsers
- 📝 TypeScript Support: Includes TypeScript definitions
npm install fast-cookie-read
The recommended way to use Fast Cookie Read is through the global window
object:
<script src="path/to/fast-cookie-read.min.js"></script>
<script>
// Recommended: Use window.FastCookieRead
window.FastCookieRead.setCookie('user', 'John');
const user = window.FastCookieRead.get('user');
</script>
// Import the library
const FastCookieRead = require('fast-cookie-read');
// or
import FastCookieRead from 'fast-cookie-read';
// Set a cookie
FastCookieRead.setCookie('user', 'John');
// Get a cookie
const user = FastCookieRead.get('user'); // Returns 'John'
- Efficient caching mechanism
- Smart parsing strategies
- Minimal DOM access
- Memory-optimized operations
- Built-in validation
- Secure cookie handling
- XSS protection
- CSRF protection
- TypeScript support
- Comprehensive error handling
- Clear documentation
- Zero dependencies
# Using npm
npm install fast-cookie-read
# Using yarn
yarn add fast-cookie-read
# Using pnpm
pnpm add fast-cookie-read
The library is available both as a module and globally via the window
object when loaded in a browser environment.
When included via script tag, the library is available globally through the window
object:
<script src="path/to/fast-cookie-read.min.js"></script>
<script>
// Recommended: Use window.FastCookieRead
window.FastCookieRead.setCookie('user', 'John');
const user = window.FastCookieRead.get('user');
</script>
const FastCookieRead = require('fast-cookie-read');
// or
import FastCookieRead from 'fast-cookie-read';
// Set a cookie
window.FastCookieRead.setCookie('user', 'John');
// Get a cookie
const user = window.FastCookieRead.get('user'); // Returns 'John'
// Remove a cookie
window.FastCookieRead.removeCookie('user');
// Check if a cookie exists
const hasUser = window.FastCookieRead.hasCookie('user'); // Returns false
// Get all cookies
const allCookies = window.FastCookieRead.getAll(); // Returns { user: 'John', ... }
// Clear all cookies
window.FastCookieRead.clearAllCookies();
Sets a cookie with the specified name and value.
FastCookieRead.setCookie('name', 'value', {
expires: 7, // days from now
maxAge: 3600, // seconds
path: '/',
domain: 'example.com',
secure: true,
sameSite: 'Strict' // 'Strict', 'Lax', or 'None'
});
Gets the value of a cookie by name. Returns null
if the cookie doesn't exist.
const value = FastCookieRead.get('name');
Returns an object containing all cookies.
const allCookies = FastCookieRead.getAll();
Removes a cookie by name.
FastCookieRead.removeCookie('name', {
path: '/',
domain: 'example.com'
});
Checks if a cookie exists.
const exists = FastCookieRead.hasCookie('name');
Removes all cookies.
FastCookieRead.clearAllCookies();
Returns the total size of all cookies in bytes.
const size = FastCookieRead.getCookieSize();
The library uses several optimizations to ensure high performance:
- Efficient Caching: Maintains an in-memory cache of parsed cookies
- Smart Parsing: Only parses cookies when necessary
- Minimal DOM Access: Reduces browser reflows
- Memory Efficient: Uses shared references where possible
The library includes comprehensive error handling:
- Validates cookie names and values
- Checks for invalid options
- Provides clear error messages
- Handles edge cases gracefully
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
- Opera (latest)
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
MIT © Nithin Murali
If you find this library helpful, please consider:
- Starring the repository
- Opening issues for bugs or feature requests
- Contributing to the project
- Supporting the project through GitHub Sponsors