Skip to content

Improvement suggestion #8

@hieunc229

Description

@hieunc229

Hi David, interesting library.

I was using a word generator with check-word. It ran a few thousand words and the NPM version couldn't handle it. I noticed that the NPM version isn't up-to-date, and the current one seems to perform much better.

I made some changes that run ~4x faster with the version on Github. It first loads the txt word list file from the old library (ie words/en.txt), then puts them into a map (faster to check if a key exists)

There is a bit of change, including the word list files, so I didn't make a PR. Though I'm happy to if you plan to use it

Here is the code in Typescript

import fs from "fs";

const possibleLanguages = ['de', 'en', 'es', 'fr'];

export default function checkword(language: string) {

    language = language && language.toLowerCase() || 'en';

    if (possibleLanguages.indexOf(language) == -1) {
        throw new Error(language + " is not valid language");
    }

    const content = fs.readFileSync(__dirname + '/words/' + language + '.txt', { encoding: "utf8" });
    const words: { [name: string]: boolean } = {};
    
    content.split(/\n/).forEach(k => {
        words[k] = true;
    });

    return {
        check: function (word: string) {

            if (word in words) {
                return true
            }

            return false;
        }
    };
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions