Sunday, January 18, 2026
HomeLanguagesJavascriptTensorflow.js tf.data.Dataset class .prefetch() Method

Tensorflow.js tf.data.Dataset class .prefetch() Method

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.

The tf.data.Dataset class .prefetch() function is used to produce a dataset that prefetches the specified elements from this given dataset.

Syntax:

prefetch (bufferSize)

Parameters: This function accepts a parameter which is illustrated below:

  • bufferSize: It is an integer value that specifies the number of elements to be prefetched.

Return Value: It returns a dataset of elements.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling the .prefetch() function over
// the specified dataset of some elements
const a = tf.data.array([5, 10, 15, 20]).prefetch(4);
  
// Getting the dataset of prefetched elements
await a.forEachAsync(a => console.log(a));


Output:

5
10
15
20

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Specifying a dataset of some elements
const a = tf.data.array(["a", "b", "c", "d", "e"]);
  
// Calling the .prefetch() function over
// the above dataset along with the 
// batch of size 2
const b = a.batch(2)
const c = b.prefetch(2)
  
// Getting the dataset of prefetched elements
await c.forEachAsync(c => console.log(c));


Output:

Tensor
    ['a', 'b']
Tensor
    ['c', 'd']
Tensor
    ['e']

Reference: https://js.tensorflow.org/api/latest/#tf.data.Dataset.prefetch

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
118 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12063 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS