I need to load a bunch of node-objects and apply some algorithm to them.
Usually I'd use an iteration and node_load() within, but I was wondering if there's a better (speaking of performance) way to load node objects.

I think that being able to cache the algorithm's result on a per-page basis would be sufficient.

2 Answers 2


node_load() is the preferable way to load a node object, especially if the node object is passed to some functions.
Take in mind that, in Drupal 6 the result of node_load() is locally cached in a static variable by default; if you are going to load a bunch of nodes, it is better if you call the function as node_load($id, NULL, TRUE). If you are sure that, for some reasons, the code could load the same node in consecutive calls to node_load(), then you can consider resetting the static value after X calls to node_load().
The result of node_load() is not cached, if the $revision parameter passed to the function is not NULL.

Caching the value obtained from node_load() is possible, and your code could use its own cache for that purpose. The cache should be invalidated when nodes are updated, which includes also the case when a node is reverted to a previous revision. Rather than implementing a cache in your own module, it would be better to use a module that caches the nodes for third-party modules.

In Drupal 7, both node_load() and node_load_multiple() accept a $reset parameter that resets the node cache kept as instance variable from the object of class NodeController that is created from Drupal, which uses the instance variable implemented by DrupalDefaultEntityController, the class that implements the default methods necessary for an entity controller.

  • Note that you would have to clear the cache every time a node is updated, not just when a new revision is created. The node itself changes, doesn't matter if there is a revision of the old version created :) And I would suggest doing caching like that only if you are re-loading the same nodes multiple times over a relatively short time span. Because, as opposed to the entity cache module linked by me, the cache will the only be used by you and won't affect "normal" node loads.
    – Berdir
    Commented Jun 1, 2011 at 7:41
  • @Berdir Entity cache is a module for Drupal 7 and that will never be back-ported to Drupal 6. Indeed, the cache should be invalidated when the node is changed; I was trying to point out that if there is a module that changes the revision of the node, the cache should be invalidated.
    – apaderno
    Commented Jun 1, 2011 at 7:52
  • @kiamluno: I know that entity cache is Drupal 7 only, that doesn't change the fact that caching only makes sense if you know hat you will re-load these nodes again (possibly multiple times) before the cache is cleared. If you don't, then the cache save is just unecessary overhead.
    – Berdir
    Commented Jun 1, 2011 at 7:56

There is no other way that I know of in Drupal 6.

Drupal 7 has node_load_multiple(), which allows to load multiple nodes in a single query (+ hooks, which can also do single queries for multiple nodes). And additinally, there is the Entity cache project, which allows to cache any loaded entity.

  • The link for Entity cache seems wrong: I get a "page not found" error when I click on it.
    – apaderno
    Commented Jun 1, 2011 at 6:37
  • @kiamlaluno: Fixed.
    – Berdir
    Commented Jun 1, 2011 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.