If you've created your own tag
Then you can use the is() function on the ItemStack.
public static final TagKey<Item> myItemTag = ItemTags.create(new ResourceLocation("mymod", "my_tag_name"));
//later...
stack.is(myItemTag);
If you want to use a vanilla tag, they are all defined for you...
stack.is(ItemTags.LOGS)
I'm not sure this results in O(1) complexity, as it may use something like contains() under the hood, but at least your code is simple and if an optimization is made, you will take advantage of it.