Array .includes vs .find vs object lookup

Benchmark created on


Setup

var valuesArray = [
    "Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape", "Honeydew",
    "Iceberg lettuce", "Jackfruit", "Kiwi", "Lemon", "Mango", "Nectarine", "Orange",
    "Papaya", "Quince", "Raspberry", "Strawberry", "Tomato", "Ugli fruit", "Vanilla",
    "Watermelon", "Xigua", "Yellow watermelon", "Zucchini", "Asparagus", "Broccoli",
    "Carrot", "Daikon", "Eggplant", "Fennel", "Garlic", "Horseradish", "Italian parsley",
    "Jalapeño", "Kale", "Leek", "Mushroom", "Napa cabbage", "Okra", "Parsnip",
    "Quinoa", "Radish", "Spinach", "Turnip", "Ube", "Vidalia onion", "Watercress",
    "Yam", "Zucchini", "Almond", "Brazil nut", "Cashew", "Doughnut", "English muffin",
    "French toast", "Gingerbread", "Hot dog", "Ice cream", "Jelly", "Kebab", "Lasagna",
    "Macaroni", "Nachos", "Omelet", "Pancake", "Quesadilla", "Ravioli", "Sushi",
    "Taco", "Udon", "Vermicelli", "Waffle", "Xylocarp", "Yogurt", "Ziti", "Apricot",
    "Blueberry", "Cantaloupe", "Dragonfruit", "Endive", "Fried chicken", "Goulash",
    "Hummus", "Iced tea", "Jambalaya", "Ketchup", "Lamb", "Meatloaf", "Noodle", "Olive",
    "Pineapple", "Quail", "Ribs", "Soup", "Turkey", "Vegetable soup", "Wheat bread"
];

var valuesObject = valuesArray.reduce((acc, e) => ({ ...acc, [e]: true }), {});

var getRandomValue = () => valuesArray[Math.floor(Math.random() * valuesArray.length)];

Test runner

Ready to run.

Testing in
TestOps/sec
.includes
var randomValue = getRandomValue();
var result = valuesArray.includes(randomValue);
ready
.find
var randomValue = getRandomValue();
var result = !!valuesArray.find(value => value === randomValue);
ready
object lookup
var randomValue = getRandomValue();
var result = !!valuesObject[randomValue];
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.